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

robbiechicago's avatar

Spatie laravel-permission teams and wire:click

I'm using the teams feature in spatie laravel-permission and it's a bit of a faff. You constantly have to ensure the correct team is being used, otherwise it doesn't work.

This is mostly handled by setting up a middleware with the setPermissionsTeamId() method in it.

However, this does not work when calling a function in a Livewire 3 component with wire:click.

Even when setting the team ID in the mount hook, the auth()-user()'s role is "forgotten" on wire:click.

I really don't want to have to manually include setPermissionsTeamId() in all methods that relate to permissions. It's bound to be forgotten at some point.

Any thoughts on how to set this globally for Livewire?

0 likes
3 replies
LaryAI's avatar
Level 58

To address the issue of ensuring the correct team is used with Spatie's Laravel Permission package in a Livewire component, you can take a few steps to make sure the setPermissionsTeamId() method is applied consistently across your application, including during Livewire interactions.

Here's a potential solution:

  1. Middleware Approach: You mentioned using middleware, which is a good start. Ensure that your middleware is correctly setting the team ID for every request. This middleware should be applied to all routes that require team-based permissions.

  2. Livewire Component Base Class: Create a base Livewire component class that all your Livewire components extend. In this base class, you can set the team ID in the mount method or any other lifecycle hook that suits your needs.

  3. Global State Management: Use a service provider or a singleton to manage the current team ID globally. This can be accessed from anywhere in your application, including Livewire components.

  4. Livewire Lifecycle Hooks: Use Livewire lifecycle hooks to ensure the team ID is set before any action is taken. For example, you can use the mount or hydrate hooks to set the team ID.

Here's an example of how you might implement this in a base Livewire component:

  1. Session Management: Ensure that the team ID is stored in the session or another persistent storage mechanism that can be accessed across requests and Livewire interactions.

By centralizing the logic for setting the team ID, you reduce the risk of forgetting to set it in individual methods. This approach also ensures that the team context is consistently applied across your application, including during Livewire interactions.

cookie's avatar

How does your middleware work? Do you use sessions for storing the teams ID?

robbiechicago's avatar

No, the team_id is stored on the user model so I'm just grabbing it from there.

Please or to participate in this conversation.