doufen3091 2017-06-23 19:39
浏览 49
已采纳

像laravel mongodb包中的多个不起作用

I have a project which is creating in laravel and mongodb. I am using a package for connecting to mongodb "https://github.com/jenssegers/laravel-mongodb". I have a table listing. In this table listing there is a text box for search data. For that I used like feature. I have followed the same in documentation but not working. When I search invoice_number data is getting empty. But when I search beds data is getting perfectly. Please correct me.

$bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
                ->where('is_delete', 0)
                ->where('invoice_number', 'like', "%{$search}%")
                ->orWhere('beds', 'like', "%{$search}%")
                ->skip($start)
                ->take($limit)
                ->orderBy($order, $dir)
                ->get();
  • 写回答

1条回答 默认 最新

  • douqi3195 2017-06-23 19:49
    关注

    The WHERE statements in your query translate to this:

    WHERE [cond_1] AND [cond_2] OR [cond_3]

    I doubt this is the condition you need. I'd say you want this:

    WHERE [cond_1] AND ([cond_2] OR [cond_3]) -- notice the brackets.

    To achieve this, you need a closure in your Builder query. Try this:

    $bookings = Booking::select('invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'txid')
            ->where('is_delete', 0)
            ->where(function($query) use ($search) { /* That's the closure */
                $query->where('invoice_number', 'like', "%{$search}%")
                ->orWhere('beds', 'like', "%{$search}%");
             })
            ->skip($start)
            ->take($limit)
            ->orderBy($order, $dir)
            ->get();
    

    And an example from the docs.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入