【问题标题】:How to solve Call to a member function department() on bool in laravel如何解决在 laravel 中调用 bool 上的成员函数部门()
【发布时间】:2019-10-23 10:05:45
【问题描述】:

我有两张表部门和科目。一个部门可以有很多学科,所以可以有很多关系。但是当我调用 $subject->department()->save($department); 时,它会显示上述错误

我的部门模型

 protected $primaryKey='dept_id';
public function subjects()
{
    return $this->hasMany(Subject::class);
}

我的主题模型

 protected $primaryKey='sub_id';
    public function department()
{
    return $this->belongsTo(Department::class);
}

在我的种子中我试试这个

$departments = array(
            array('name' => 'Bachelor of Business Administration'),
            array('name' => 'Bachelor of Computer Science and Engineering'),
            array('name' => 'Bachelor of Science in Civil Engineering'),
            array('name' => 'Bachelor of Science in Mechanical Engineering'),
            array('name' => 'Bachelor of Electrical & Electronics Engineering'),
            array('name' => 'Bachelor of Science in Nursing'),
            array('name' => 'Bachelor of Arts in Tourism and Hospitality Management'),
            array('name' => 'Bachelor of Science in Agriculture'),
            array('name' => 'Bachelor of Arts in Economics'),
        );
        Department::insert($departments);
        $subjects = array(
            array('name' => 'Software'),
            array('name' => 'Networking'),
        );
        $department=Department::where('name','Bachelor of Computer Science and Engineering')->first();
        $subject= Subject::insert($subjects);
        $subject->department()->save($department);

但我总是收到这个错误

请帮我解决这个问题我想用他们的部门ID保存所有科目。

【问题讨论】:

    标签: php laravel eloquent-relationship


    【解决方案1】:

    如我所见,您使用了 $subject = Subject::insert($subjects);这将返回 bool(0 或 1)作为您的插入函数的结果,并且您正在尝试调用 department() 关系,这就是它给出错误的原因。

    【讨论】:

    • 那我应该怎么做才能解决我的问题,或者我将如何使用 department_id 保存主题
    • 试试这个 foreach ($subjects as $subject) { $sub = new Subject(); $sub->name = $subject['name']; $sub->保存(); $sub->dealerships()->save($department); } insted of $subject= Subject::insert($subjects); $subject->department()->保存($department);
    【解决方案2】:

    我明白了,问题出在 "::insert()" 它将返回布尔值作为真/假。

    我建议使用 create() 而不是 insert()。但是使用 create() 将无法一次添加多行,因此您应该在 "$subjects" 数组上创建一个 foreach 循环并将其一一添加。

    你需要改变你的方法,这里是参考代码。

    $department = Department::where('name','Bachelor of Computer Science and Engineering')->first();
    
    $subjects = array(
        array('name' => 'Software'),
        array('name' => 'Networking'),
    );
    
    foreach ($subjects as $subject) {
        $department->subjects()->create($subject);
    }
    

    【讨论】:

    • foreach($subjects as $subject) { $sub = Subject::create($subject); $sub->department()->保存($department); }
    • 调用未定义的方法 Illuminate\Database\Eloquent\Relations\BelongsTo::save()
    • 使用 associate 解决了问题,但部门 id si 没有保存在数据库中
    • 尝试使用 create() 而不是 save();
    • 它给了我错误,因为关系创建不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 2021-08-18
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多