2016-07-11 5 views
0

Mein Problem ist ein bisschen mystisch. Die Ausgabe des SQL-Codes funktioniert, wenn ich es mit PHPmyadmin versuche, aber es funktioniert nicht mit Laravel. HierLaravel - Syntaxfehler oder Zugriffsverletzung: 1064 Sie haben einen Fehler in Ihrer SQL-Syntax

ist der Query Builder Code

$product= Product::leftJoin('ProductTransaction as PT',function($query){ 
       $query->on('PT.ProductId','=','Product.id'); 
       $todaysDate=Carbon::today()->toDateString(); 
       $query->Where(DB::raw("date(`PT`.`CreatedAt`)=$todaysDate")); 

      })->Where(function($query) use($RetailerIds){ 
       if (count($RetailerIds)) { 
        $query->WhereIn('RetailerId',$RetailerIds); 
       } 
      })->Where(function($query)use($Genders){ 
       if (count($Genders)) { 
        $query->WhereIn('Gender',$Genders); 
       } 
      })->WhereHas("Stocks",function($query) use($Color){ 

       if ($Color!='') { 
        $query->WhereHas('Color',function($query) use($Color){ 
         $query->Where('Color','like',"%$Color%"); 
        }); 
       } 

      })->Where(function($query) use ($q){ 

       if ($q!='') { 
        $query->Where('ProductTitle','like','%'.$q.'%'); 
        $query->orWhere('Description','like','%'.$q.'%'); 
       } 

      })->WhereHas('Stocks',function($query) { 
       $query->Where('Stock','!=',-1); 
       $query->WhereDate('CreatedAt','=',Carbon::today()->toDateString()); 
      })->WhereHas('ProductTransactions',function($query) use($MinDiscountPrice,$MaxDiscountPrice,$todaysMaxDiscountPrice,$todaysMinDiscountPrice){ 

       $query->WhereDate('Product.CreatedAt','=',Carbon::today()->toDateString()); 

       if ($todaysMinDiscountPrice!=$MinDiscountPrice) { 
        $query->Where('DiscountPrice','>=',$MinDiscountPrice); 
       } 

       if ($todaysMaxDiscountPrice!=$MaxDiscountPrice) { 
        $query->Where('DiscountPrice','<=',$MaxDiscountPrice); 
       } 
      })->With(['ProductTransactions','ProductImages','ProductCategory','Retailer','Stocks.Size'])->OrderBy($orderBy)->paginate(15); 

Hier wird der Fehlercode, dass diese Abfrage auffüllt:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? where (select count(*) from `Stock` where `Stock`.`ProductId` = `Product`.`id`' at line 1 (SQL: select count(*) as aggregate from `Product` left join `ProductTransaction` as `PT` on `PT`.`ProductId` = `Product`.`id` and date(`PT`.`CreatedAt`)=2016-07-11 where (select count(*) from `Stock` where `Stock`.`ProductId` = `Product`.`id`) >= 1 and (`ProductTitle` like %test% or `Description` like %test%) and (select count(*) from `Stock` where `Stock`.`ProductId` = `Product`.`id` and `Stock` != -1 and date(`CreatedAt`) = 2016-07-11) >= 1 and (select count(*) from `ProductTransaction` where `ProductTransaction`.`ProductId` = `Product`.`id` and date(`Product`.`CreatedAt`) = 2016-07-11) >= 1) 

Aber interessanteste Punkt ist hier, wenn ich kopieren und einfügen, die SQL-Code ist im Fehlertext funktioniert es. Hier ist der SQL-Code, dass ich es einfügen PHPMyadmin

SELECT 
    count(*) AS AGGREGATE 
FROM 
    `Product` 
LEFT JOIN `ProductTransaction` AS `PT` ON `PT`.`ProductId` = `Product`.`id` 
AND date(`PT`.`CreatedAt`) = 2016 - 07 - 11 
WHERE 
    (
     SELECT 
      count(*) 
     FROM 
      `Stock` 
     WHERE 
      `Stock`.`ProductId` = `Product`.`id` 
    ) >= 1 
AND (
    `ProductTitle` LIKE % test % 
    OR `Description` LIKE % test % 
) 
AND (
    SELECT 
     count(*) 
    FROM 
     `Stock` 
    WHERE 
     `Stock`.`ProductId` = `Product`.`id` 
    AND `Stock` != - 1 
    AND date(`CreatedAt`) = 2016 - 07 - 11 
) >= 1 
AND (
    SELECT 
     count(*) 
    FROM 
     `ProductTransaction` 
    WHERE 
     `ProductTransaction`.`ProductId` = `Product`.`id` 
    AND date(`Product`.`CreatedAt`) = 2016 - 07 - 11 
) >= 1 
) 

Antwort

1

Ihre Daten müssen zitiert werden. Übergeben Sie es als eine Variable in stattdessen oder zitieren sie:

$query->Where(DB::raw("date(`PT`.`CreatedAt`)='$todaysDate'")); 

ODER

$query->Where(DB::raw("date(`PT`.`CreatedAt`)=?",[$todaysDate])); // Preferable, so that the database can handle it 
+0

Es tut mir leid, beide Code hilft mir nicht. – fobus

+0

Erhalten Sie immer noch den gleichen mysql Fehler an der gleichen Stelle? – aynber

+0

Ja, immer noch der gleiche Fehler. – fobus

Verwandte Themen