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

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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀