2017-01-26 39 views
1

我正在使用以下查詢來獲取數據。當我將參數傳遞給where子句時,它將參數轉換爲問號?在將參數傳遞給laravel 5查詢生成器時的子句錯誤

$productDatas = DB::table('products') 
         ->select('products.*','products.id as pid','product_features.*','product_features.id as pfid') 
         ->join('product_features','product_features.product_id','=','products.id') 
         ->join('categories','categories.id','=','products.cat_id','left'); 

if($value == 13) { 
    // $value = intval($value); 
    $productData = $productDatas->where('product_features.'.$field,'<',$value)->toSql(); 
    dd($productData); 
} else { 
    $productData = $productDatas->where('product_features.'.$field,'=',$value)->get(); 
} 

輸出查詢:

"select `products`.*, `products`.`id` as `pid`, `product_features`.*, `product_features`.`id` as `pfid` 
from `products` 
inner join `product_features` on `product_features`.`product_id` = `products`.`id` 
left join `categories` on `categories`.`id` = `products`.`cat_id` 
where `product_features`.`primary_camera` < ?" 
+2

問題是什麼? –

+0

問題沒有從數據庫獲取實際數據 –

+0

dd($ productData)是幹什麼的?在你的'else'塊中,你正在調用' - > get()'函數,但是你沒有在'if'塊中這樣做。 – akousmata

回答

0

不要擔心?馬克。這是怎麼laravel表明你會在?的地方有一個動態分配的值。

爲了在您的相應路徑文件中充分演示下面的代碼並運行代碼。

\DB::listen(function ($sql, $bindings, $time) { 
    dump($sql); 
    dump($time); 
    dump($bindings); 
}); 

這會告訴你什麼是將放置在其中?是地方$bindings。此外,您可以列出查詢執行$time