Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nikocraft's avatar

Using Policies in blade

I am following this https://laravel.com/docs/5.3/authorization Where it says:

Actions That Don't Require Models
Like most of the other authorization methods, you may pass a class name to the 
@can and @cannot directives if the action does not require a model instance:

@can('create', Post::class)
    <!-- The Current User Can Create Posts -->
@endcan

If I put that in my blade create method of the policy does not get executed at all. I had to change Post::class to 'App\Post'. Any ideas why? I would like to use Post::class it looks much better. In my PostController I do import the Post model like use App\Post; Do I need to do something more to be able to use Post:class

0 likes
5 replies
martinbean's avatar

@maxnb You’re asking two questions here.

  1. It may not work because either the path to your user model is wrong, or you have not added a binding to your AuthServiceProvider.php file, so the permission check will always return false.

  2. You have to use the FQN (fully-qualified name) for the model (App\Post) instead of Post::class because Blade templates are executed in the “global” namespace.

nikocraft's avatar

I have added the binding

    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
        Post::class => PostPolicy::class,
    ];

If I have to use FQN in blade files then why does Laravel 5.3 documentation says otherwise when giving the example?

CGuy's avatar

Same issue here. Laravel Docs only show the class name without namespace. Anybody knows how to make it work this way?

Please or to participate in this conversation.