Home > front end >  Laravel Eloquent - Return Where Has Where
Laravel Eloquent - Return Where Has Where

Time:12-04

I have customers that have many parcels through orders.

public function parcels()
    {
        return $this->hasManyThrough(Parcel::class, Order::class);
    }

I want to return customers that have parcels more than 0 with successful status only. But I don't know how to add conditions of parcel status. Currently, this statement gives me a list of customers that have parcels. $customers = Customer::has('parcels', '>' , 0)->get();

CodePudding user response:

You can use the whereHas method.

$customers = Customer::whereHas('parcels', function ($query) {
    $query->where('status', 'successful');
})->get();
  • Related