1 回答

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超6個贊
例如,將Post與當(dāng)前用戶相關(guān)聯(lián)(例如,在將Post保存到db的控制器方法內(nèi)部)可以像以下操作:
// controller method
public function savePost( Request $request )
{
$data = $request->validate([
// your validation rules here..
]);
$post = new Post(); // from Post model
$post->fill($data);
$post->author()->associate(\Auth::user()); // relate post to current user
$post->save(); // save to db
return view('whatever_your_view_is', []);
}
如果您需要更多信息,請發(fā)表評論
在Post模型中進(jìn)行以下更改:
public function author()
{
return $this->belongsTo(User::Class, 'author_id'); // specify the column which stores the author in posts table
}
- 1 回答
- 0 關(guān)注
- 309 瀏覽
添加回答
舉報