简体   繁体   English

如何在 laravel

[英]How to solve Call to a member function department() on bool in laravel

I have two tables departments and subjects.我有两个表部门和科目。 One department can have many subject, so it can be hasMany relationship.一个部门可以有很多学科,所以可以有很多关系。 But when i call $subject->department()->save($department);但是当我调用$subject->department()->save($department); it shows me the above error它向我显示了上述错误

MY department model MY系model

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

My subject Model我的主题 Model

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

And in my seedin i try this在我的种子中,我试试这个

$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);

But i am always getting this error但我总是收到这个错误在此处输入图像描述

Please help me to solve this problem I want to save all the subjects with their department ids.请帮我解决这个问题我想用他们的部门ID保存所有科目。

As I can see you have used $subject = Subject::insert($subjects);如我所见,您使用了 $subject = Subject::insert($subjects); which will return bool (0 or 1) as result of your insert function and you are trying to call the department() relation on that, thats why its giving error.作为插入 function 的结果,这将返回 bool (0 或 1),而您正试图在其上调用 department() 关系,这就是它给出错误的原因。

I see, the issue is with "::insert()" It will return boolean value as true/ false.我明白了,问题出在“::insert()”上,它将 boolean 值返回为真/假。

I would suggest to use create() instead of insert().我建议使用 create() 而不是 insert()。 However with the use of create() you won't able to add multiple row at a single time, So there you should create a foreach loop on "$subjects" array and add it there one by one.但是,使用 create() 您将无法一次添加多行,因此您应该在“$subjects”数组上创建一个 foreach 循环并将其一一添加。

You need to change your approach, Here is the ref code.你需要改变你的方法,这是参考代码。

$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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM