简体   繁体   English

WhereBetween 在 Laravel 5.2 中不起作用

[英]WhereBetween not working in laravel 5.2

I am using laravel framework but in this WhereBetween not working.我正在使用 laravel 框架,但在这个 WhereBetween 中不起作用。 i am using price range where price starts form 1 to 500. when i set price 1-100 it gives me all the records that is in beetween 1 to 100 ie 20,40,50 etc. When i change the value from 1- 150 the above result will not be displayed it gives me no result(20,40,50).我使用的价格范围从 1 到 500上面的结果将不会显示它给我没有结果(20,40,50)。 Can anyone help me .谁能帮助我。 Here is my Code这是我的代码

enter code here

     $products = Product::with("productimages")
                ->whereBetween('price',[$data['from'], $data['to']])
                 ->get();

Note:- $data['from'] start value ie 1 and $data['to'] end value ie 150 or more注意:- $data['from'] 起始值即 1 和 $data['to'] 结束值即 150 或更多

I just had the same issue.我只是有同样的问题。 When the data type of the value is not an integer, it will behave very unpredictably.当值的数据类型不是整数时,它的行为将非常不可预测。 So the solution is to either change the datatype of the column or cast it while fetching, like this:因此,解决方案是更改列的数据类型或在获取时强制转换,如下所示:

$from = $data['from'];
$to = $data['to'];

$products = Product::with("productimages")
    ->whereRaw("CAST(price AS UNSIGNED) BETWEEN ${from} AND ${to}")
    ->get();

I actually had this issue on the PostgreSQL jsonb data type, as it always returns nested values as strings, and between fetching doesn't work properly.我实际上在 PostgreSQL jsonb 数据类型上遇到了这个问题,因为它总是将嵌套值作为字符串返回,并且在获取之间无法正常工作。

Using Where Between使用中间位置

$projects = Product::where('created_at', '>', $data['from'])
->where('created_at', '<', $data['to'])    
->get();

OR

  $current = Product::with("productimages")
  ->whereBetween('created_at', array($data['from'], $data['to']))->get();

OR

       DB::table('Product')->whereBetween('created_at', array($data['from'], $data['to']))->get();

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

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