有两种不同处理的雄辩的laravel查询条件

问题描述 投票:0回答:1

这是我尝试做的事情:

$pipes = Pipe::with('items')
            ->where([
                'user_id' => $request->get('id'),
                'closed' => false,
            ])
            ->whereHas('items', function($query) {
                $query->where(function($q) {
                    $q->where('payment_id', 'exists', false)
                      ->where('closed', false)
                      ->where('locker', 'exists', false);
                })
                ->orWhere(function($qu) {
                    $qu->where('payment_id', 'exists', true)
                    ->where('closed', false)
                    ->where('locker', 'exists', false)
                    ->where('failed_to_pay', true);
                });

            })->get();

我需要在'项'上的位置使用两种不同的处理方法:

->where('payment_id', 'exists', false)
->where('closed', false)
->where('locker', 'exists', false);

OR

/* The principal difference is 'If payment_id exist, i take where failed_to_pay is true' */
->where('payment_id', 'exists', true)
->where('closed', false)
->where('locker', 'exists', false) 
->where('failed_to_pay', true);

我的问题是怎么做?非常感谢你!

php laravel eloquent laravel-query-builder
1个回答
0
投票

请尝试使用orWhere()

 $pipes = Pipe::with('items')
            ->where([
                'user_id' => $request->get('id'),
                'closed' => false,
            ])
            ->whereHas('items', function ($query) {
                $query->where(function ($q) {
                    $q->where('payment_id', 'exists', false)
                        ->orWhere('payment_id', 'exists', true)
                        ->where('closed', false)
                        ->where('locker', 'exists', false)
                        ->orWhere('failed_to_pay', true);
                });

            })->get();
© www.soinside.com 2019 - 2024. All rights reserved.