Developer Snippet Diary

Add where conditions in subquery laravel

$orders = \App\Models\Woocommerce_orders::select('woocommerce_orders')
            ->with('tasks')
            ->orderByDesc('woo_id');

$orders = $orders->whereDoesntHave('tasks', function ($q) {     // Exclude rows having ANY pending status in sub table
        $q->where('status', 'pending');
    })
    ->whereDoesntHave('tasks', function ($q) {     // Exclude rows having ANY NULL statuses
        $q->whereNull('status');
    })
    ->with(['tasks' => function ($q) {     // Eager load the tasks (all clean ones since bad ones are excluded already)
        $q->latest('updated_at');
    }]);
Posted by: R GONDAL
Email: rizikmw@gmail.com