Laravel Elixir Collection License

How to apply laravel Gate ( http://laravel.com/docs/5.1/authorization ) for an eloquent collection.

It works for a single item as below

$post = Post::findOrFail($id); if ($user->cannot('view-post', $post)) { abort(403); } 

But does not work for the collection. Is it possible to filter a collection using Gate and return the collection?

 $posts = Post::all(); 
+5
source share
1 answer

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

0
source

Source: https://habr.com/ru/post/1232552/


All Articles