I’m trying to use Policies inside a Post Component, using Laravel. This is how I’m iterating through the posts in my page.
@foreach($posts as $post)
<x-post
:id="$post->id"
:title="$post->title"
:description="$post->description"
:userId="$post->user_id"
:img="$post->img"
:author="$post->user->name"/>
@endforeach
In the post.blade.php I’m trying to use a ‘update’ policy method to define which users can see the post:
@can('update', Auth::user(), /*What shoud I pass here?*/)
<a class="btn btn-success"href="{{route('posts.edit', $id)}}">
<i class="bi bi-pencil"></i>
</a>
@endcan
What should I pass as the second parameter of the policy? Normally, it would be a post variable. Since I’m already inside a post, I don’t know to proceed.
>Solution :
You could check outside the component. Something like
@foreach ($posts as $post)
<x-post
:id="$post->id"
:title="$post->title"
:description="$post->description"
:userId="$post->user_id"
:img="$post->img"
:author="$post->user->name"
:canUpdate="Auth::user()->can('update', $post)"/>
@endforeach
@if ($canUpdate)
<a class="btn btn-success"href="{{ route('posts.edit', $id) }}">
<i class="bi bi-pencil"></i>
</a>
@endif