How to use @can correctly in blade template using strategy
P粉821231319
P粉821231319 2024-04-04 13:51:55
0
1
477

I am unable to create @can()

in the blade template as the documentation suggests

This is my policy:

public function update(User $user, Canal $canal): bool {
  return ($canal->user->id == $user->id) and ($user->hasPermissionTo('actualizar canal'));
}

I'm using Spatie permissions. Anyway, this strategy works if I protect the route in the controller as:

public function edit(Request $request, Canal $canal) {
    $this->authorize('update', $canal);
    return view('Canal/edit', ['canal' => $canal]);
  }

Now, my problem is with the blade. I want to conditionally render a button to edit $canal, I'm trying to do this:

@can('update', App\Models\Canal::class)
    <x-gui.link-button href="{{ route('canal.edit', $canal->id) }}" value="Modificar" />
  @endcan

This is exactly what the documentation says. But I get an error which says another parameter is required in the call:

Too few arguments to function App\Policies\CanalPolicy::update()

So I guess I also have to send to the user in @can(), I changed it to:

@can('update', Auth::user(), App\Models\Canal::class)
    <x-gui.link-button href="{{ route('canal.edit', $canal->id) }}" value="Modificar" />
  @endcan

This doesn't work either, this doesn't "invoke" the strategy at all. how could I know? I put some Log::info() in there.

Any ideas?

P粉821231319
P粉821231319

reply all(1)
P粉451614834

To resolve this issue, send $code instead of App\Models\Canal::class on the second argument to @can, For example:

@can('update', $canal)
    <x-gui.link-button href="{{ route('canal.edit', $canal->id) }}" value="Modificar" />
@endcan
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template