Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ignore records hasMany

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can have use doesntHave for this

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

User::doesntHave('orders')->get();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading