How to apply laravel Gate (http://laravel.com/docs/5.1/authorization) for an eloquent collection.
It works for single item like below
$post = Post::findOrFail($id);
if ($user->cannot('view-post', $post)) {
abort(403);
}
But not working for a collection. Is it possible to filter the collection using Gate and return a collection?
$posts = Post::all();
I have the same question. Maybe some like this:
$post->filter(function($value,$key){
if(\Gate::allows('view-post',$value)){
return $val;
}
});
but there must be a better solution.
Related links:
https://laravel.com/docs/5.2/collections#method-filter
https://laravel.com/docs/5.2/authorization
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With