Let's assume I've got a User model and the Country model. User could have up to four countries simultaneously (home country and three countries he'd like to visit). Using Eloquent, I'd create a pivot able country_user with an extra field of type, that would correspond to the choice of the user, which country he'd put in the first place etc. So once again, there is:
User
--id
Country
--id
country_user
--id
--user_id
--country_id
--type
I need to get all users who have, let's say, Canada as a country type 1, UK as a country type 2, France as a country type 3 and New Zealand as a country type 4. If I understand correctly then I can't use wherePivot to do this, only if I get a User::all() and iterate through this collection (which doesn't make much sense and puts unnecessary load on the DB). What could be the proper way to do this with a minimum possible amount of queries to DB? Or do I get it all wrong and there should be another approach to this task at all?
You can still search on thepivot
table using therelationship
you have defined between aUser
andCountry
. If for the sake of this example we call that relationshipcountries
and that you want to find people with the following$criteria
(sticking to your example):
$criteria = [
'canada' => 1,
'uk' => 2,
'france' => 3,
'new zealand' => 4,
];
So thekey
in the above is theCountry
and thevalue
is thetype
.
To findUsers
that have those exact requirements, you could do the following:
$users = User::whereHas('countries');
collect($criteria)->each(function ($type, $country) use ($users) {
$users->whereHas('countries', function (Builder $query) use ($type, $country) {
$query->where(['name' => $country, 'type' => $type]);
})->get();
});
Then do something with your$users
.
$users->get();
You can the country relationship ofHasMany
So, in users it would be this
public function countries() {
return $this->hasMany('country_user_class', 'foreign_key_id_here')->latest()->take(4); // take 4 since the user can have 4 countries
}
The query would look something like this.
User::whereHas('countries', function($query) {
$query->where('type', [1,2,3,4]);
})->get();
This would give you the result. This is how you can achieve it using Query. Such a problem can always be solved by Query. Let me know if you've any more question regarding this.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.