Laravel 11.21
Livewire 3.5
Spatie Laravel Permission 6.9
Spatie Laravel Permissions WITH TEAMS
The issue
I'm encountering an issue where the teamId is null after a dispatch/emit in Livewire components. This results in checking for permissions returning wrong values (like i'm having no permissions at all).
Steps To Reproduce
Create a Laravel app with Livewire, spatie permissions and teams
Create a Livewire page, and a Livewire component and use the component inside the page.
Add a button to the Livewire component which is only enabled for a specific permission
No, other pages are still aware of the teamId, just that specific component lost it after a dispatch/emit or wire:init. Lost like, getPermissionsTeamId() is returning null
Looks like it has something to do with a livewire/update call I'm seeing in my console (sail up). A dispatch/emit or wire:init trigger this livewire/update as well
For example:
<button wire:click="save" @cannot('update', $vacancy) disabled @endcannot>{{ __('Save') }}</button>
The button is not disabled, so the user has permissions to update a vacancy, but if I check the permissions inside the save function with auth()->user()->cannot('update', $this->vacancy) it is false, logging the teamId results in null 🙃
I finally figured it out. My middleware was wrongly placed. In my own words: It was after the navigation but before the liveware update, or something. So a Livewire update did not know the teamId. Thanks for thinking along!
Description
We are using the following versions:
Laravel 11.21 Livewire 3.5 Spatie Laravel Permission 6.9 Spatie Laravel Permissions WITH TEAMS
The issue I'm encountering an issue where the teamId is null after a dispatch/emit in Livewire components. This results in checking for permissions returning wrong values (like i'm having no permissions at all).
Steps To Reproduce
Another way:
Example Application
No response
Version of spatie/laravel-permission package:
6.9
Version of laravel/framework package:
11.21
PHP version:
8.2
Database engine and version:
No response
OS: Windows/Mac/Linux version:
No response
Is the session being lost?
No, other pages are still aware of the teamId, just that specific component lost it after a dispatch/emit or wire:init. Lost like, getPermissionsTeamId() is returning null
Looks like it has something to do with a
livewire/update
call I'm seeing in my console (sail up
). A dispatch/emit or wire:init trigger this livewire/update as wellFor example:
<button wire:click="save" @cannot('update', $vacancy) disabled @endcannot>{{ __('Save') }}</button>
The button is not disabled, so the user has permissions to update a vacancy, but if I check the permissions inside the save function with
auth()->user()->cannot('update', $this->vacancy)
it is false, logging the teamId results in null 🙃I finally figured it out. My middleware was wrongly placed. In my own words: It was after the navigation but before the liveware update, or something. So a Livewire update did not know the teamId. Thanks for thinking along!
Yes, that fully makes sense. Thanks for posting back the solution.