【发布时间】: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