或者哪里和哪里有组合不在laravel工作

时间:2017-09-18 08:23:20

标签: laravel eloquent

$orders = Order::where([
                ['user_account_id', Auth::user()->user_account_id],
                ['order_status_id', '1']
            ])->orWhere([
                ['user_account_id', Auth::user()->user_account_id],
                ['order_status_id', '5']
            ])->whereHas('event', function($query) {
                $query->where('end_date', '>', Carbon::now());
            })->paginate(10);

在我的查询whereHas中,如果使用orWhere则部分不起作用,否则很好

它有什么问题?求助。

2 个答案:

答案 0 :(得分:1)

尝试

$orders =   Order::where(function($query){
                $query->where(
                    ['user_account_id', Auth::user()->user_account_id],
                    ['order_status_id', '1']
                ])->orWhere([
                    ['user_account_id', Auth::user()->user_account_id],
                    ['order_status_id', '5']
                ])})
            ->whereHas('event', function($query) {
                $query->where('end_date', '>', Carbon::now());
            })->paginate(10);

OR

$orders =   Order::where('user_account_id', Auth::user()->user_account_id)
            ->whereIN('order_status_id',[1,5])
            ->whereHas('event', function($query) {
                $query->where('end_date', '>', Carbon::now());
            })->paginate(10);

答案 1 :(得分:0)

另一个解决方案可能是使用whereIn方法检查值是否在给定数组中。这可以防止您根本不需要使用orWhere。

    $orders = Order::where('user_account_id', Auth::user()->user_account_id)
        ->whereIn('order_status_id', ['1', '5'])
        ->whereHas('event', function($query) {
            $query->where('end_date', '>', Carbon::now());
        })->paginate(10);
相关问题