简体   繁体   English

将参数传递给laravel 5查询构建器时的Where子句错误

[英]Where clause error in passing parameter to laravel 5 query builder

I am getting data using the below query. 我使用以下查询获取数据。 When I pass the parameter to where clause, it converts the parameter to a question mark ? 当我将参数传递给where子句时,它将参数转换为问号? .

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

Output of query: 输出查询:

"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` < ?"

Do not worry about the ? 不用担心? mark. 标记。 It's how laravel indicate you there is gonna be a dynamically assigned value at the place where the ? 这是laravel如何表明你将在哪个位置动态分配值? is. 是。

To demonstrate fully in you corresponding route file place the following code and run the code. 要在您对应的路径文件中完整演示,请放置以下代码并运行代码。

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

This will show you what are the $bindings that will placed at the places where the ? 这将显示将放置在哪些地方的$bindings ? are. 是。 Additionally you can listed to the query execution $time 此外,您可以列出查询执行$time

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM