dtujfmfs06058 2019-04-11 11:42
浏览 47

如何在雄辩的关系中添加2个中间表

How to write below query in eloquent form. Eloquent seems good with the 2 table relationship, but with 4 table relationship and group_concat it is not working properly. The below code is working fine, just need to know how can I achieve the below code in eloquent format.

Relation goes like this
Tables > Columns :
mrs > id, name
mr_regions > id, mrs_id(mrs table), regions_id(regions table)
regions > id, name, headquaters_id(headquaters table)
headquaters > id, name

DB::table('mrs as m')
->select(
    'm.id',
    'first_name',
    'last_name',
    'email',
    'designation',
    'mobile_number', 
    DB::raw('GROUP_CONCAT(DISTINCT r.name SEPARATOR ", ") AS regions'),
    DB::raw('GROUP_CONCAT(DISTINCT h.name SEPARATOR ", ") AS headquaters')
)
->join('mr_regions AS mr', function($join){
    $join->on('mr.mrs_id', '=', 'm.id');
    $join->on('mr.is_active', '=', DB::raw(Config('constants.STATUS_ACTIVE')));
})
->join('regions AS r', function($join){
    $join->on('r.id', '=', 'mr.mrs_id');
    $join->on('mr.is_active', '=', DB::raw(Config('constants.STATUS_ACTIVE')));
})
->join('headquaters AS h', function($join){
    $join->on('h.id', '=', 'r.headquaters_id');
    $join->on('h.is_active', '=', DB::raw(Config('constants.STATUS_ACTIVE')));
})
->where(function($where) use ($id){
    $where->where('m.is_active', Config('constants.STATUS_ACTIVE'));
    $where->where('m.id', '=', $id);
})
->groupby('m.id')
->first();
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题