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.

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

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏