⌘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 | Technical Writer

Oussama is an experienced full-stack web developer with a strong focus on Laravel. He's passionate about crafting web applications with Filament and the TALL Stack. With 8+ years of experience, and he's a dedicated open-source contributor.

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.