I want to save new post from form in my database , I have title and body in $request and want to save logged in user's id in the post. how can i handle it in model or validation?
Validator::make($request->all(), [
'title' => ['required'],
'body' => ['required'],
])->validate();
Post::create($request->all());
I can save it as below:
$post = new Flight;
$post->title = $request->title;
$post->body = $request->body;
$post->user_id = $request->user()->id;
$post->save();
but I want better way.
- user_id is not nullable in DB