Home > other >  ignore records hasMany
ignore records hasMany

Time:06-08

I want to get all users which have no records in Orders Table.

Here is My Code

//User Model

 public function orders()
 {
     return $this->hasMany(Order::class, 'user_id');
 }

User::all(); //query

Thank You

CodePudding user response:

You can have use doesntHave for this

https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-absence

User::doesntHave('orders')->get();
  • Related