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

谁都会走 提交于 2020-01-30 09:29:05

问题


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

$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','d.destination_id as destinationId','d.name as destinationName', 't.type',
                'tf.name as officeName', 'ag.name as agencyName', 'u.name as userName', 'v.name as vehicleName')
            ->join('destinations as d', function ($join){
                $join->on('t.from_destination_id','=','d.destination_id')
                    ->orOn('t.to_destination_id','=','d.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();

i want to get the names of each id after this results

$searchResults = $bookingTransfersData
            ->where('periodStart','between', $periodStart && $periodEnd)
            ->where('periodEnd','between', $periodStart && $periodEnd)
            ->where('destinationName','=',$from_destination_name && $to_destination_name)->where('type','like', $type);

like:

$fromDestinationName = $searchResults->pluck('from_destination_id','destinationName')
            ->where('from_destination_id','=','destinationId');

but $fromDestinationName return an empty collection

please help :)


回答1:


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 :)



来源:https://stackoverflow.com/questions/59886057/how-to-retrieve-different-names-from-same-table-with-different-ids-on-join-larav

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!