英文:
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->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, 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('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 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('update', $canal)
<x-gui.link-button href="{{ route('canal.edit', $canal->id) }}" value="Modificar" />
@endcan
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论