【问题标题】:Laravel DB facade select dynamicalyLaravel DB 外观动态选择
【发布时间】:2018-11-18 13:47:28
【问题描述】:

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

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

            ])
            ->get();

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

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

【问题讨论】:

  • “但我不能在请求中执行此操作”是什么意思?您遇到的具体问题是什么?
  • 哦,抱歉@MichaelKunst,我现在没看到没问题,我不知道如果我的查询中有条件,我该怎么办;)

标签: database laravel join select laravel-facade


【解决方案1】:
$query = DB::table ('enterpriseAddress');

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

【讨论】:

  • 什么是结果
  • 进行中,我会告诉你;)
  • 它正在工作,我只是对所有请求都这样做,这就是为什么需要这么长时间。感谢您的帮助。
  • 其实不是,我没看到没有结果。
  • 发生了什么?
猜你喜欢
  • 2019-03-27
  • 2016-09-27
  • 1970-01-01
  • 2019-01-19
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多