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

I want to convert my raw query to query builder

我是 laravel 的新人 实际上,我有一个 auth table,根据用户类型,它是 link 到另一个 table。我想要来自相应 tables 的用户信息,所以我现在使用原始查询,我想将其转换为查询生成器,请帮助

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

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

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

这是文档的 link。 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();