Using Spatie Permission Package with Livewire

I’m Using Spatie Permission With Laravel livewire, but when I check the role in livewire component it’s through middleware does not exist error

code sample

    public function mount()
    {
        $this->middleware('permission:role-list');
    }

I saw Livewire Authorization but I don’t know what I’m missing here?

any help I’ll be appreciated.

Any hints?
@Dimonka2 @Snapey

$this->middleware is not a valid function?

You need to use the authorization() function as per the docs?

Both is not an option here.
did you work with spatie permission package in livewire before?

or any suggestions on how to do it correctly?

maybe apply the middleware on the route directly in web.php would work ??

Hey, @jihadismail8 Thank you so much for your response.

yeah, you are right about using the middleware directly inside the routes file, but I want a better way to implement the permission inside the controller directly.

This does not make sense for me as middleware should be a part of a route definition. One possible way is to make a normal route with middleware and add your component to a blade. There is also Livewire route function, but I am not sure whether it is possible to add middleware there:

1 Like

In normal Laravel application, we can call the middleware method in the construct controller class to make your app a little bit cleaner.

But in livewire, we can’t add it to the mounting method.

In v.2 of Livewire there are no more Livewire route function. I know, now it still exist in docs but take a look at this:
https://laravel-livewire.com/docs/2.x/upgrading#route-livewire