alamizo alamizo - 3 months ago 14
PHP Question

Laravel trying to get user id to store into posts table

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.

Answer

Get the authed user.

$article->user_id = Auth::user()->id;
$article->save();
Comments