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

时间:2017-01-26 12:10:09

标签: php laravel laravel-5

我使用以下查询获取数据。当我将参数传递给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();
}

查询输出:

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

1 个答案:

答案 0 :(得分:0)

不要担心?标记。这就是laravel如何表明在?所在的地方将会有一个动态分配的值。

要在相应的路径文件中完整演示,请放置以下代码并运行代码。

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

这将显示$bindings放置在?所在位置的$time。此外,您可以列出查询执行cat /proc/cpuinfo processor : 0 vendor_id : GenuineIntel cpu family : 6 model : 63 model name : Intel(R) Xeon(R) CPU E5-2676 v3 @ 2.40GHz stepping : 2 microcode : 0x25 cpu MHz : 2400.070 cache size : 30720 KB physical id : 0 siblings : 1 core id : 0 cpu cores : 1 apicid : 0 initial apicid : 0 fpu : yes fpu_exception : yes cpuid level : 13 wp : yes flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm xsaveopt fsgsbase bmi1 avx2 smep bmi2 erms invpcid bogomips : 4800.14 clflush size : 64 cache_alignment : 64 address sizes : 46 bits physical, 48 bits virtual power management: