我想将原始查询转换为查询生成器

时间:2019-06-20 08:14:04

标签: laravel postgresql query-builder

我是Laravel的新朋友 实际上,我有一个身份验证表,该表根据用户类型链接到其他表。我要从相应的表中获取用户信息,所以我现在使用原始查询,我想将其转换为查询生成器,请帮助

$data = DB::select("SELECT srr.id,srr.created_at,srr.fromid,srr.toid,srr.from_usertype,au.firstname_admin,au.lastname_admin,cd.name as to_compayname,COALESCE(unionSub1.firstname,NULL) as from_firstname, unionSub1.lastname as from_lastname
                 from service_request_reviews as srr
                 left join (
                     (select authid, firstname, lastname from userdetails)
                     union (select authid, firstname, lastname from yachtdetail)
                     union (select authid, firstname, lastname from talentdetails)
                     union (select authid, name as firstname, COALESCE(NULL,NULL) as lastname from companydetails)
                 ) unionSub1 on unionSub1.authid = srr.fromid
                 left join auths as au on au.id = srr.fromid
                 LEFT JOIN companydetails as cd ON cd.authid = srr.toid WHERE srr.isdeleted = '0' AND srr.parent_id1 = '0' " );

我已经尝试过此方法,并且可以在不合并的情况下正常工作。我不知道如何在左联接中使用多个联合。

$data = DB::table('service_request_reviews as srr')
       ->select('srr.id','srr.created_at','srr.fromid','srr.toid','srr.from_usertype','au.firstname_admin','au.lastname_admin','cd.name as to_compayname')
->leftjoin('auths as au', 'au.id', '=' ,'srr.fromid')
                ->leftjoin('companydetails as cd', 'cd.authid', '=', 'srr.toid')
 ->where('srr.isdeleted', '0')
 ->where('srr.parent_id', '0');

1 个答案:

答案 0 :(得分:2)

您可以将联合定义为该表的查询构建器,例如:

$yachtdetail = DB::table("yachtdetail")
                    ->select('authid', 'firstname', 'lastname');

$talentdetails = DB::table('talentdetails')
                    ->select('authid', 'firstname', 'lastname');

现在您可以使用:

$data = DB::table('service_request_reviews as srr')
       ->select('srr.id','srr.created_at','srr.fromid','srr.toid','srr.from_usertype','au.firstname_admin','au.lastname_admin','cd.name as to_compayname')
       ->leftjoin('auths as au', 'au.id', '=' ,'srr.fromid')
       ->leftjoin('companydetails as cd', 'cd.authid', '=', 'srr.toid')
       ->where('srr.isdeleted', '0')
       ->where('srr.parent_id', '0')
       ->union($yachtdetail)
       ->union($talentdetails)
       ->get();

这是文档的链接。 https://laravel.com/docs/5.8/queries#unions

已编辑:

您可以尝试以下方法:

$queryBuilder = DB::table('service_request_reviews as srr')
       ->select('srr.id','srr.created_at','srr.fromid','srr.toid','srr.from_usertype','au.firstname_admin','au.lastname_admin','cd.name as to_compayname')
       ->leftjoin('auths as au', 'au.id', '=' ,'srr.fromid')
       ->leftjoin('companydetails as cd', 'cd.authid', '=', 'srr.toid')
       ->leftjoin(DB::raw("((select authid, firstname, lastname from userdetails)
                     union (select authid, firstname, lastname from yachtdetail)
                     union (select authid, firstname, lastname from talentdetails)
                     union (select authid, name as firstname, null as lastname from companydetails)) as unionSub1"), function($join){

                        $join->on(DB::raw('unionSub1.authid'), '=', DB::raw('srr.fromid'));
       })
       ->where('srr.isdeleted', '0')
       ->where('srr.parent_id', '0');

$data = $queryBuilder->get();