how to retrieve different names from same table with different ids on join laravel

前端 未结 1 766
耶瑟儿~
耶瑟儿~ 2021-01-24 15:11

I need to get names from destinations table for each from_destination_id and to_destination_id as fromDestinationName and toDestinationName

$bookingTransfersData         


        
1条回答
  •  [愿得一人]
    2021-01-24 15:49

    I solved it by removing this join:

    ->join('destinations as d', function ($join){
                    $join->on('t.from_destination_id','=','d.destination_id')
                        ->orOn('t.to_destination_id','=','d.destination_id');
                })
    

    and add for each destionation_id a join to retrive each name and this will not work if I don't add for table name that i joined two times as to name it new name like 'destinations as d1' and 'destinations as d2'

    $bookingTransfersData = DB::table('transfers as t')
                ->select('t.periodStart as periodStart', 't.periodEnd as periodEnd','t.days','t.transfer_id','t.cost_round_trip',
                    't.cost_one_way','t.status','d1.destination_id as fromDestinationId','d1.name as fromDestinationName', 't.type',
                    't.to_destination_id','tf.name as officeName', 'ag.name as agencyName', 'u.name as userName', 'v.name as vehicleName',
                    't.from_destination_id', 'd2.destination_id as toDestinationId','d2.name as toDestinationName')
                ->join('destinations as d1','t.from_destination_id','=','d1.destination_id')
                ->join('destinations as d2','t.to_destination_id','=','d2.destination_id')
                ->join('vehicles as v','t.vehicle_id','=','v.vehicle_id')
                ->join('transfer_offices as tf','t.office_id','=','tf.transfer_office_id')
                ->join('agencies as ag','t.forAgency_id','=','ag.agency_id')
                ->join('users as u','t.addedBy_user_id','=','u.id')->get();
    

    the problem solved :)

    0 讨论(0)
提交回复
热议问题