Laravel DB外观选择dynamicaly

时间:2018-06-08 13:25:17

标签: database laravel join select laravel-facade

我正在尝试使用Laravel中的db facade进行动态选择。

$EntrepriseSurBAseDuNace = DB::table ('enterpriseAddress')
            ->join('enterpriseEnterprise', 'enterpriseEnterprise.EnterpriseNumber', '=', 'enterpriseAddress.EntityNumber','LEFT OUTER')
            ->select('EntityNumber')
            ->where([
                ['Zipcode','=',$CodePostal],
                ['enterpriseEnterprise.StartDate    ','=',$DateCreation],

            ])
            ->get();

如果数组/ Vars不为空,我想添加连接,如下所示:

if(isset($Var){
->join ...... 
}

if(isset($Array){
->join ...... 
}

但我无法在请求中执行此操作。 Thak的帮助和对不起我的英语。

感谢大卫:

    $queryTest = DB::table ('enterpriseAddress');
    if (isset($RechercheRequete['CodePostal']))
    {
        $CodePostal = $RechercheRequete['CodePostal'];

        $queryTest->where('enterpriseAddress.Zipcode','=', $CodePostal);

    }



    $result = $queryTest->select('EntityNumber') ->get(); dd($result); 
    exit(); 

1 个答案:

答案 0 :(得分:0)

$query = DB::table ('enterpriseAddress');

if (your-condition) {
   $query->join(...
} elseif ('other condition') {
   $query->join(...
}
$query->select('EntityNumber')
    ->where([
         ['Zipcode','=',$CodePostal],
         ['enterpriseEnterprise.StartDate    ','=',$DateCreation],
     ])->get();