如何在使用策略时,在Blade模板中正确使用@can?

huangapple go评论56阅读模式
英文:

How do you correctly use @can in blade templates using policies

问题

I'm not able to make the @can() in a blade template as the documentation suggest

Here's 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 policy works if I protect the routes 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 the $canal. I'm trying this:

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

This is exactly as the docs say. But I'm getting an error that says it needs another parameter in the call:

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

So I'm guessing I have to send the user also in the @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; it simply doesn't "call" the policy. How do I know? I placed some Log::info() there.

Any idea?

英文:

I'm not able to make the @can() in a blade template as the documentation suggest

Here's my policy:

public function update(User $user, Canal $canal): bool {
  return ($canal-&gt;user-&gt;id == $user-&gt;id) and ($user-&gt;hasPermissionTo(&#39;actualizar canal&#39;));
}

I'm using Spatie Permissions. Anyway, this policy works, if I protect the routes in the controller as:

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

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

  @can(&#39;update&#39;, App\Models\Canal::class)
    &lt;x-gui.link-button href=&quot;{{ route(&#39;canal.edit&#39;, $canal-&gt;id) }}&quot; value=&quot;Modificar&quot; /&gt;
  @endcan

This is exactly as the docs say. But I'm getting an error, it says it needs another parameter in the call:

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

So I'm guessing I have to send the user also in the @can(), I change it to:

  @can(&#39;update&#39;, Auth::user(), App\Models\Canal::class)
    &lt;x-gui.link-button href=&quot;{{ route(&#39;canal.edit&#39;, $canal-&gt;id) }}&quot; value=&quot;Modificar&quot; /&gt;
  @endcan

This doesn't work either, this simply doesn't "call" the policy. How do I know? I placed some Log::info() there.

Any idea?

答案1

得分: 0

为了解决这个问题,请在@can的第二个参数中发送$code,而不是App\Models\Canal::class,例如:

@can('update', $code)
    <x-gui.link-button href="{{ route('canal.edit', $code->id) }}" value="Modificar" />
@endcan
英文:

To fix this, send the $code instead of App\Models\Canal::class on the 2nd param of @can like:

@can(&#39;update&#39;, $canal)
    &lt;x-gui.link-button href=&quot;{{ route(&#39;canal.edit&#39;, $canal-&gt;id) }}&quot; value=&quot;Modificar&quot; /&gt;
@endcan

huangapple
  • 本文由 发表于 2023年5月7日 04:52:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定