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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题