The section below takes the user's session data, uses the posts
method present in the User
model to create a new post in the database.
I then retrieve the id
of the created record and associate this record with a category by means of the pivot
table:
if(\Auth::user() && $dbId = \Auth::user()->posts()
->create($request->except(['categoria']))) {
$post = Post::find($dbId->id);
$post->categorias()->attach($request->input('categoria'));
return redirect('/painel/post')
->with(['status' => 'success', 'msg' => 'Post criado com sucesso!']);
}
But I'm wondering if this would be the best solution?
The code for this model is available at: PostController