I am using Laravel 11.
In a blade file, I have this directive :
@can('delete', $work)
@endcan
And in my workPolicy file, I have this function
public function delete(User $user, Work $work): bool
{
Log::info('I delete a work');
}
my problem is that the $work variable in my blade file is not of type Work. And so the delete method is never called.
So I would like to be able to “parse” or something like that the $work variable before calling the @can directive.
How to do that ?
Of course if I add this,it works :
@php
$workPolicy = AppModelsWork::find($work->id)
@endphp
@can('delete', $workPolicy)
but it is one more request !
4
If your delete logic depends on $work
, than you must pass the variable. If you logic does not depend on $work
, you can use following code in blade.
@can('delete', AppModelsWork::class)
<!-- The current user can delete -->
@endcan
More information can be found here