0

i am trying to make an dynamical select with db facade in Laravel.

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

            ])
            ->get();

And i would like to add join if arrays/Vars aren't empty, something like this :

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

or

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

but i can't do this in the request. Thak's for help and sorry for my english.

Thanks to David :

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

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

    }



    $result = $queryTest->select('EntityNumber') ->get(); dd($result); 
    exit(); 
karl
  • 5
  • 4

1 Answers1

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();
Davit Zeynalyan
  • 8,418
  • 5
  • 30
  • 55
  • what is results – Davit Zeynalyan Jun 08 '18 at 13:35
  • in progress, i ll tell you ;) – karl Jun 08 '18 at 13:36
  • it's working , I just do it to all the request that's why it takes so long. Thank's for your help. – karl Jun 08 '18 at 14:01
  • Not really in fact, i didn't see that there is no result. – karl Jun 08 '18 at 14:12
  • what is happen? – Davit Zeynalyan Jun 08 '18 at 14:16
  • i am maybe an idiot cause i need to do the query and not just print it as a result right or the "->get()" execute the request ? – karl Jun 08 '18 at 14:18
  • it's the way i need to go my problem is in my side i think ;) so your answer is right . – karl Jun 08 '18 at 14:25
  • Also all time you can use dd($query->toSql()) to see your current query – Davit Zeynalyan Jun 08 '18 at 14:29
  • i check the request and it looks ok but it didn't return me the result. That's my query : "select `EnterpriseNumber` from `enterpriseEnterprise` inner join `enterpriseDenomination` on `enterpriseDenomination`.`EntityNumber` = `enterpriseEnterprise`.`EnterpriseNumber` inner join `enterpriseAddress` on `enterpriseAddress`.`EntityNumber` = `enterpriseEnterprise`.`EnterpriseNumber` where `enterpriseDenomination`.`Language` = ? and `enterpriseEnterprise`.`TypeOfEnterprise` = ? and `enterpriseAddress`.`Zipcode` = ? ◀" and i receive a full page with everything Laravel can write but not my result ^^ – karl Jun 08 '18 at 14:41
  • I am sorry but i cannot discribe this better for the moment ^^. – karl Jun 08 '18 at 14:45
  • All time nested query is not better solution. you must be avoid to use nested query. If it is possible – Davit Zeynalyan Jun 08 '18 at 14:50
  • Sorry David but it looks like not working :/ i edited my post. – karl Jun 08 '18 at 14:53
  • $result = $queryTest->select('EntityNumber') ->get(); dd($result); exit(); – Davit Zeynalyan Jun 08 '18 at 14:57
  • hope i make u laugh a bit :D , sorry for my noobie skill thanks for your patience – karl Jun 08 '18 at 15:01
  • David sorry but i wanna do nested query instead of a query of each table, cause the informations for where clauses are all on a different table. Is it better to do a request to db for each where clause ?( i just saw ur comment about that)We discuss a bit about that with a colleague and he tells me to decrease the number of request with join. – karl Jun 08 '18 at 15:07