1
The section below takes the user’s session data, uses the method posts
present in the model User
to create a new post in the bank.
Later I recover the id
of the created record and associate this record to a category through the table pivot
:
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 not sure if this is the best solution?
The code for this model is available in: Postcontroller