⌘K
Noting Found

View all related articles

Fetch Records Based on Relationship condition

Snippets 1 min read

If you want to get records based on relationship condition, you can use whereHas method.

Here is an example:

1$allUsers = User::count() // 1000 [male/female/others]
2 
3$usersWithMaleGender = User::with('profile')
4 ->active() // scope for where('status', 1)
5 ->whereHas('profile', function ($query) {
6 $query->where('gender', 'male');
7 })->count(); // 650

the records of the result are all the users who has the male gender.


Related Tags

About the Author

Oussama's Profile Picture
Oussama
Full Stack Web Developer

I'm a full stack web developer and telecommunications engineer who love to share knowledge and build stuff online!


Comments

Join our newsletter

Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

We care about the protection of your data. Read our Privacy Policy.