Home > database >  Trying to use Laravel policies into components
Trying to use Laravel policies into components

Time:02-03

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 href="{{route('posts.edit', $id)}}">
           <i ></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.

CodePudding user response:

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 href="{{ route('posts.edit', $id) }}">
        <i ></i>
    </a>
@endif
  •  Tags:  
  • Related