dongyuling0312 2016-08-26 02:49
浏览 15
已采纳

Laravel试图将用户ID存储到posts表中

public function store(Request $request)
{
   $this->validate($request, array(
       'title' => 'required|max:255|min:2',
       'slug' => 'required|alpha_dash|unique:articles,slug',
       'category_id' => 'required|integer',
       'content' => 'required|min:2'
   ));

   $article = new Article;
   $article->title = $request->title;
   $article->slug = $request->slug;
   $article->category_id = $request->category_id;

   $article->content = $request->content;
   $article->save();

   Session::flash('success', 'Your article has been published.');
   return redirect()->route('article.show', $article->id);
}

There is a user_id column on the articles table,

This is my store function but when I create the post I am looking for a way to store the logged in user id to the user_id column in the articles table.

  • 写回答

3条回答 默认 最新

  • dpy87530 2016-08-26 03:00
    关注

    Get the authed user.

    $article->user_id = Auth::user()->id;
    $article->save();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?