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

nillie_io's avatar

Adding Roles to models other than user

Hi Laracast community,

I am spinning my wheels here and would appreciate any assistance. Quick background- I currently have a web app that I am re-building with Laravel + Inertia. I am a Vue dev, and have made it pretty far with my build.

One complaint with my existing app that I am addressing with this build is that parents of my application wanted to use a single email address for multiple of their kids to access content that they have been registered for. So, ex: [email protected] can have multiple accounts - kid 1 account, kid 2 account, parent 1 account, etc. After logging in with the [email protected], the user can select an account and the application saves the acccount_id in the current session.

I have my relationships built out, where a User:class hasMany Account::class, and an Account::class belongsTo a User::class.

Everything was fine and dandy until now, where I am spinning my wheels trying to figure out how to apply a role to an Account, not a User. I've switched back and forth between Spatie permissions and Siber's Bouncer, but every example I see is along the lines of $user->assign($role). I need something more like $user->accounts->where('id',4)->assign($role);

If anyone can help point me in the correct direction I'd forever be grateful. Is this even possible? I know that I can figure things out as soon as I just figure out how to assign a role to an account.

Thank you sincerely,.

1 like
7 replies
LaryAI's avatar
Level 58

To assign a role to an account, you can create a new model, say AccountRole, that belongs to an Account and has a role_id field. Then, you can use the Spatie permissions package to assign roles to AccountRole models. Here's an example:

// AccountRole.php
class AccountRole extends Model
{
    protected $fillable = ['role_id'];
    
    public function account()
    {
        return $this->belongsTo(Account::class);
    }
    
    public function role()
    {
        return $this->belongsTo(Role::class);
    }
}

// Account.php
class Account extends Model
{
    public function user()
    {
        return $this->belongsTo(User::class);
    }
    
    public function roles()
    {
        return $this->hasMany(AccountRole::class);
    }
}

// Assigning a role to an account
$account = Account::find(4);
$role = Role::findByName('admin');
$account->roles()->create(['role_id' => $role->id]);

This way, you can assign roles to accounts instead of users.

Snapey's avatar
Snapey
Best Answer
Level 122

have you looked at the team extension to Spatie permission?

If you assume account and team are the same thing, this allows a parent to have different permissions or roles in different accounts.

In an organisation, I might be a team manager in one team, (eg can approve holidays for staff) but only user role in another team.

Apologies if this is not quite mapping to your scenario

1 like
nillie_io's avatar

@Snapey Hi Snapey, I did manage to make progress using Spatie Permissions and the teams "addon."

I followed their docs and renamed "team_id" with account_id, added the middleware, etc. Now I'm finally moving forward! Just have to create some global roles, seed some permissions, and figure out how the heck policies work and I think I'll be golden. Thank you for the input.

2 likes
dcx's avatar

There could be a number of ways to go about this, I would probably explore manyTomany relation between accounts and roles which exposes a number of helper methods for example hasManyThrough()/attach()/detach()/sync() I hope it's at least a steer in the right direction...

It's in the Laravel docs of course but also check this guy out at link below - a lot of very helpful content, including on this topic, like you say there isn't that much I could find on many to many either, great feature (in my view) once it clicks though..

https://www.youtube.com/@LaravelDaily/search?query=many%20to%20many

1 like
nillie_io's avatar

@dcx Thank you DCX. I did manage to get this working through the Spatie Permission package per my response above. I appreciate the links to some many to many content. I'll check that out. All the best.

1 like
jlrdw's avatar

@nillie_io I read through whole post, please if solved mark as solved. I just use if (in_array......

dcx's avatar

@nillie_io the suggestion from @snapey was better if your not wanting to build from the ground up (makes sense in a lot of cases) - glad to see you found a way to move forward - good luck in rest of the project! :)

Please or to participate in this conversation.