Home  >  Q&A  >  body text

How to use @can correctly in blade template using strategy

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粉821231319P粉821231319171 days ago324

reply all(1)I'll reply

  • P粉451614834

    P粉4516148342024-04-05 10:10:24

    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

    reply
    0
  • Cancelreply