在数据透视表中插入数据时遇到一些问题。
当我使用return dd($request->education);
时,我已经成功获取了数组。
但是当我使用attach
foreach($ request-> education为$ education)
{
$ preq-> education()-> attach([
'education_id'=> $教育
]);
}
要么
$ preq-> education()-> attach([
'education_id'=> $ request->教育
]);
遇到错误Call to a member function attach() on null
这是我的Preq模型
class Preq extends Model {
protected $table = 'preqs';
public function education()
{
$this->belongsToMany(Education::class)->withTimestamps();
}
}
最佳答案
要链接,您需要将对象从education()
返回
class Preq extends Model {
protected $table = 'preqs';
public function education()
{
return $this->belongsToMany(Education::class)->withTimestamps();
}
}