dacastro4
dacastro4

Reputation: 373

Trying to get 3er level laravel relationship

Let me put the code first..

Permissions Model

class Permission extends Model {
    public function roles()
    {
        return $this->belongsToMany('App\Role');
    }

    public function menus()
    {
        return $this->belongsTo('App\Menu');
    }
}

Role Model

class Role extends Model {

    public function users()
    {
        return $this->hasMany('App\User');
    }

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

Menu Model

class Menu extends Model {
    public function permissions()
    {
        return $this->hasMany('App\Permission');
    }
}

User Model

class User extends Authenticatable {    
    public function role()
    {
        return $this->belongsTo('App\Role');
    }

 }

Well, I'm trying to get something like (assuming that I already got a single user) $user->role->permissions->menus I'm trying to get role, then the permissions associated with that role and then the menu items that are associated with that permissions.

Idk if i'm explaining myself..

Upvotes: 2

Views: 37

Answers (1)

oseintow
oseintow

Reputation: 7371

$users = App\User::with('role.permissions.menus')->get();

Upvotes: 2

Related Questions