'What is the eloquent query to get users who has a field value and this value is part of given array?
I have users records like that
| id | fields |
|---|---|
| 1 | ["fashion","commercial"] |
| 2 | ["commercial"] |
and I want to get the users how has at least one field form that array ["fashion", "food", "sport"]
Solution 1:[1]
Although you could use whereIn() like:
$users = DB::table('users')
->whereIn('fields', ["fashion", "food", "sport"])
->get();
I don't think that it would return anything because of your "fields" column structure. Probably the easiest way is to get all the users and filter them using array_intersect(), it would look something like this:
foreach($users as $user){
if (array_intersect($user, $fields)) {
$filteredUser[] = $user;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pedrogcs |
