【问题标题】:How to join multple coulmns in one table with the other table in laravel如何将一个表中的多个列与laravel中的另一个表连接起来
【发布时间】:2021-01-11 12:32:54
【问题描述】:
business_id supported_by reccomended_by approved_by
312 1 2 3
staff_id name address phone
1 xxx yyy 158931478
2 aaa lll 158958936
3 ccc 12wee 154531478

我想将第一个表中的supported_by、reccomended_by 和approved_by 列与第二个表的staff_id 连接起来,并想“选择”他们的名字。我该怎么做?

【问题讨论】:

    标签: laravel


    【解决方案1】:

    # Advanced Join Clauses

    $staffs = DB::table('businesses')
        ->select('staffs.name')
        ->join('staffs', function ($join) {
            $join->on('businesses.supported_by', '=', 'staffs.staff_id')
                ->orOn('businesses.reccomended_by', '=', 'staffs.staff_id')
                ->orOn('businesses.approved_by', '=', 'staffs.staff_id');
        })
        ->where('businesses.business_id', '=', 312)
        ->get();
    
    foreach ($staffs as $staff) {
        $staff->name
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 1970-01-01
      相关资源
      最近更新 更多