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

Darkus62's avatar

Join 3 tables

I have the models permission_role, permission and modulo. I would like to do a join with the three tables.

I am using this join with the tables role_user and permission I am getting the permissions, i mean is working fine, but I would like to do a join with modulo too.

$permissions = Permission::leftJoin('usuario.permission_role', function($join) use ($id)
                                            {
                                                $join->on('permissions.id', '=', 'permission_role.permission_id');
                                                $join->on('permission_role.role_id','=',DB::raw($id));
                                            })
                       ->select('permissions.id','permissions.name','permissions.slug','permissions.description','permissions.id_modulo','permissions.id_submodulo','permission_role.role_id')
                       ->get();

I want to make a join with the result of permissions and the table modulo These are the models:

Permission:

public function modulo()
    {
        return $this->belongsTo('efsystem\Modulo', 'id_modulo','id');
    }
    
    public function submodulo()
    {
        return $this->belongsTo('efsystem\Submodulo', 'id_submodulo','id');
    }

Permission_role:

   public function role()
    {
        return $this->belongsTo('Caffeinated\Shinobi\Models\Role;','role_id','id');
    }

    public function permission()
    {
        return $this->belongsTo('Caffeinated\Shinobi\Models\Permission','permission_id','id');
    }

Modulo:


    public function permission()
    {
        return $this->hasMany('efsystem\Modulo', 'id_modulo','id');
    }

(Sorry for my bad english)

0 likes
0 replies

Please or to participate in this conversation.