I have an user page where I show all the posts by that user.
This page can be accessed by other users more or less like instagram.
Now the point is I want to show in every single post in that page a list for options available to the user who posted and not the other users.
I checked the documentation, all the examples are on pages that show one post (my.project/post/1) let's say, so obviously that is received through a get method and in the view you can add the @can
method and define the gate
in the controller that spits out the post to that page.
But what if I am sending all the posts to the page like this:
public function index()
{
$user = Auth::user();
$posts_with_comments = Post::with(['comments.author',
'user' => function ($q) {
$q->select('id', 'username');
},
'some_stuff'])->where('user_id', $user->id)->get()->reverse();
return view('user.page')->with('posts',$posts_with_comments);
}
I registered a policy like this:
class PostPolicy
{
/**
* @param User $user
* @param Post $post
* @return bool
*/
public function deletePost(User $user, Post $post)
{
return $user->id === $post->user_id;
}
in AuthServiceProvider
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
\App\Post::class => \App\Policies\PostPolicy::class
];
/**
* Register any application authentication / authorization services.
*
* @param \Illuminate\Contracts\Auth\Access\Gate $gate
* @return void
*/
public function boot(GateContract $gate)
{
parent::registerPolicies($gate);
}
}
so how do I manage the gate
stuff in the controller to achieve my goal?