how to retrieve different names from same table wi

2020-04-19 05:50发布

问题:

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