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

LaravelLover's avatar

Laravel 3 table relationship

i need some help with laravel relationship as im bit confused with which relationship to use here..

So i have 3 tables

  1. Roles

i) id ii) name

  1. Permissions

i) id ii) name

  1. role_has_permissions

i) role_id ii) permission_id

Single role can have multiple permissions

how do i manage my relationship for those 3 tables?

Any help is really appreciated!

0 likes
4 replies
Punksolid's avatar
Level 25

Hi @laravellover

I bet is a many to many relationship


Class Role extends Model{

     public function permissions() {
        return $this->belongsToMany(App\Permission::class, 'role_has_permissions');
    }
}


Class Permission extends Model{

     public function roles() {
        return $this->belongsToMany(App\Role::class,  'role_has_permissions');
    }
}

So lets say yo want to retrieve all the permissions name of a role

$role->permissions()->pluck('name')->toArray();

Maybe you will need to set the third and fourth parameter of the belongsToMany (https://laravel.com/docs/master/eloquent-relationships#many-to-many)

janosk's avatar

@laravellover Glad it worked!

Please mark best reply (no hard feelings if it is not mine :)) so the thread will be listed as solved. Thank you!

Please or to participate in this conversation.