dongxu7121 2018-02-01 06:54
浏览 368
已采纳

Laravel 5中的多个where语句

I am trying to implement filter search result through ajax in laravel 5.4 Currently i am facing a problem with whereBetween when combined with other where statement.

I have an query statement as below:

    $query   = $this->hotel->query();
    $query   = $query->where(function ($query){
                    $query->orWhereBetween('min_price' , [500 , 1000]);
                    $query->orWhereBetween('min_price' , [5000 , 50000]);
                });
    $query   = $query->whereIn('internet', [1,2]);
    $query   = $query->whereIn('breakfast', [1,2]);
    $query   = $query->where('language', 'LIKE', '%'.'english'.'%');

    $hotel = $query->get();
    foreach($hotel as $h) {
        echo $h->id.'<br>';
    } exit();

The above query doesn't list the result of price between 500 to 100 and 5000 to 50000 There are two rows matching the conditions above but it return only single row.

Database data

id    min_price    internet    breakfast    language
4     876          2           2            english
5     40000        1           2            english 

Result: The above query result only id 4, but should return both ids 4 and 5.

I think I am missing something on line

$query   = $query->where(function ($query){
                    $query->orWhereBetween('min_price' , [500 , 1000]);
                    $query->orWhereBetween('min_price' , [5000 , 50000]);
                });

How can I achieve correct result? Please help.

  • 写回答

2条回答 默认 最新

  • donglie7778 2018-02-01 07:06
    关注

    Not sure with your requirements but you may need to arrange your where conditions like

    $query   = $query->orWhere(function ($q){ //assuming b/w is not mandatory
                        $q->whereBetween('min_price' , [500 , 1000]); // removed OR from here
                        $q->orWhereBetween('min_price' , [5000 , 50000]);
                    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?