I would recommend going a step deeper, and creating Route Model Binding:
So in your app/Providers/RouteServiceProvider.php:
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot(Router $router)
{
$router->bind(
'posts',
function ($routeArgument) {
if(\Auth::user()->cannot('see_all')) {
return \Auth::user()->posts()->findOrFail($routeArgument);
}
return Post::findOrFail($routeArgument);
}
);
parent::boot($router);
}
Remember to import your Post model. :)
This gains to two things:
Read more about Route Model Binding here.
This is a good solution but I still have a problem...
On my PostsController :
public function index() {
if(Auth::user()->cannot('see_all'))
$post= Auth::user()->posts;
else
$post= Post::all();
}
This cannot be replaced by your solution :/ That's why I think it's better to use scope, no?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community