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 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏