【发布时间】:2018-05-27 00:26:36
【问题描述】:
我在数据透视表中插入数据时遇到了一些问题。
当我使用 return dd($request->education); 时,我已经成功获取了数组。
但是当我使用附加时
foreach($request->education as $education) { $preq->教育()->附加([ '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();
}
}
【问题讨论】:
-
您缺少
return语句。并确保添加一个带有教育类路径的 use 语句:use App\Education。您还应该在解决后关闭它。
标签: php mysql pivot-table laravel-5.4 laravel-eloquent