2016-07-11 8 views
0

私の問題は少し神秘的です。私がPHPmyadminで試してみると、sqlコードの出力は機能しますが、laravelで動作しません。ここでLaravel - 構文エラーまたはアクセス違反:1064 SQL構文にエラーがあります

クエリビルダコードここ

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

は、このクエリが移入というエラーコードです:

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) 

しかし、最も興味深い点は、ここで私はコピーして貼り付けるとSQLコードがされていますエラーテキストでは動作します。 PHPMyadminに貼り付けている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 
) 

答えて

1

日付を引用する必要があります。代わりに、変数としてそれを渡し、またはそれを引用:

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

OR

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

私は申し訳ありませんが、コードの両方が私を助けていません。 – fobus

+0

同じ時点で、まったく同じmysqlエラーが発生していますか? – aynber

+0

はい、同じエラーです。 – fobus

関連する問題