我在将数据插入数据透视表时遇到一些问题。
当我使用 return dd($request->education);
我已经成功获取了数组。
但是当我使用附加时
foreach($request->education as $education) { $preq->education()->attach([ 'education_id' => $education ]); }
或
$preq->education()->attach([ 'education_id' => $request->education ]);
出现一些错误在 null 上调用成员函数attach()
这是我的 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();
}
}
关于php - Laravel 5.4 在数据透视表中插入时调用 null 上的成员函数 Attach(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47787527/