如何在联接Laravel上从具有不同ID的同一张表中检索不同名称 [英] how to retrieve different names from same table with different ids on join laravel

查看:208
本文介绍了如何在联接Laravel上从具有不同ID的同一张表中检索不同名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从目标表中获取每个from_destination_id和to_destination_id的名称 如fromDestinationName和toDestinationName

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();

我想在此结果后获取每个ID的名称

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');

$fromDestinationName返回空集合

请帮助:)

推荐答案

我通过删除此联接来解决它:

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');
            })

并为每个destionation_id添加一个联接以检索每个名称 如果我不添加两次以 as 联接的表名来命名新名称,则此方法将不起作用 'destinations as d1''destinations as d2'

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();

问题解决了:)

这篇关于如何在联接Laravel上从具有不同ID的同一张表中检索不同名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆