I have the following middleware:
class OwnerOrAdmin
{
public function handle($request, Closure $next)
{
$user = \Auth::user();
$postId = $request->route('post');
$post = $user->posts->find($postId);
if ($user->id == 1 || (!is_null($post) && $post->user_id == $user->id)) {
return $next($request);
}
return redirect()->route('post.index')
->with(['status' => 'danger', 'mensagem' => 'Este post não te pertence!']);
}
}
Basically this check if the user who will edit a post is Admin (id = 1) or if this is the owner of the post.
But in my controller I have to repeat practically the same query to the bank:
public function edit($id)
{
$post = Post::find($id);
return view('painel.post.cad-edit', compact('post'));
}
Would it be possible to pass this value to the controller as a middleware return?