尝试在组件中使用 Laravel 策略

Trying to use Laravel policies into components

我正在尝试在 Post 组件中使用策略,使用 Laravel。这就是我在页面中遍历 post 的方式。

       @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

在 post.blade.php 中,我正在尝试使用 'update' 策略方法来定义哪些用户可以看到 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

我应该将什么作为策略的第二个参数传递?通常,它将是一个 post 变量。由于我已经在 post 中,我不知道如何继续。

您可以检查组件外部。像

@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