mardi 4 juin 2019

Query builder proper sintax for 4 joins + 3 where clauses (Laravel5.7)

I'm trying to get this MySQL query to work in laravel 5.7 query builder. It works fine in phpmyadmin

SELECT c.Symbol
, s.SectorName
, cprs.strenght
, s.parentid
, ssbpi.Risklevel 
, ssbpi.ColumnType
FROM Companies AS c
JOIN Sectors AS s ON s.SectorID = c.SectorID
JOIN Company_PriceRS AS cprs ON cprs.CompanyID = c.CompanyID
JOIN SubSectorsBPIsData AS ssbpi ON ssbpi.subcategoryid = s.parentid
WHERE cprs.PostDate = '2017-05-08'
AND WHERE CompanyPriceRS.strenght = 'strong'
AND WHERE SubSectorsBPIsData.ColumnType = $ColumnType 


ColumnType is a variable that comes from a dropdown and it's already being captured and working properly.

I have tried the normal way according to documentation:

$Completequerytry1 = DB::table('Companies')
 ->join('Sectors', 'Sectors.SectorID', '=', 'Companies.SectorID')
 ->join('CompanyPriceRS', 'CompanyPriceRS.CompanyID', '=', 'Companies.CompanyID')  
 ->$join('SubSectorsBPIsData ', 'SubSectorsBPIsData.subcategoryid', '=', 'Sectors.parentid')

 ->where('CompanyPriceRS.strenght', '=', 'strong')    
 ->where('SubSectorsBPIsData.ColumnType', '=', $ColumnType) 
 ->where('CompanyPriceRS.Postdate', '=', '2017-05-08'); 
 ->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
  ->limit(10);
->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
 ->limit(10);
echo '<pre>';
print_r($Completequerytry1);

Error: Symfony\Component\Debug\Exception\FatalThrowableError thrown with message "syntax error, unexpected '->' (T_OBJECT_OPERATOR)"

  1. Using functions With several nested joins:
$Completequerytry1 = DB::table('Companies')
->join('Sectors', function ($join) use ($ColumnType) {
 $join->on('Sectors.SectorID', '=', 'Companies.SectorID')   
->join('CompanyPriceRS', function ($join2) { 
  $join2->on('CompanyPriceRS.CompanyID', '=', 'Companies.CompanyID')  
  ->join('SubSectorsBPIsData', function ($join3)  { 
  $join3->on('SubSectorsBPIsData.subcategoryid', '=', 'Sectors.parentid')

 ->where(function ($query1)  {
   $query1->where('CompanyPriceRS.strenght', '=', 'strong')  //filter 1
          ->where('SubSectorsBPIsData.ColumnType', '=', $ColumnType) //filter2
           ->where('CompanyPriceRS.Postdate', '=', '2017-05-08'); // filter 3
        });
    });    
});
})
->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
 ->limit(10);
echo '<pre>';
print_r($Completequerytry1);

Error: ErrorException (E_NOTICE) Undefined variable: ColumnType

3: Then tried functions with nested WHERE

$Completequerytry1 = DB::table('Companies')
 ->join('Sectors', 'Sectors.SectorID', '=', 'Companies.SectorID')
 ->join('CompanyPriceRS', 'CompanyPriceRS.CompanyID', '=', 'Companies.CompanyID')  
 ->$join('SubSectorsBPIsData ', 'SubSectorsBPIsData.subcategoryid', '=', 'Sectors.parentid') //ERROR IS GIVEN ON THIS LINE

 ->where(function ($query1)  {
 $query1->where('CompanyPriceRS.strenght', '=', 'strong')    
         ->where('SubSectorsBPIsData.ColumnType', '=', $ColumnType) 
         ->where('CompanyPriceRS.Postdate', '=', '2017-05-08'); 
   });

->select('Companies.Symbol', 'Sectors.SectorName', 'CompanyPriceRS.strenght', 'Sectors.parentid', 'SubSectorsBPIsData.subcategoryid','SubSectorsBPIsData.ColumnType')
 ->limit(10);
echo '<pre>';
print_r($Completequerytry1);

Error: Undefined variable: join

Still don't know what i'm missing. Should i create functions for the JOINs and the WHEREs ? Running out of ideas. Thanks in advance for your insights :)



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2WJ2Ej6
via IFTTT

Aucun commentaire:

Enregistrer un commentaire