Searching a Laravel collection
I had a Laravel collection and I wanted to search it for some content. Surely there is way?
Let's say you have a collection of users and you want to find all users whose age is greater than 25. You can use the filter()
method of the collection to achieve this.
<?php
$users = collect([
['name' => 'John Doe', 'age' => 30],
['name' => 'Jane Doe', 'age' => 20],
['name' => 'Bob Smith', 'age' => 40],
['name' => 'Alice Jones', 'age' => 25],
]);
$filteredUsers = $users->filter(function ($user) {
return $user['age'] > 25;
});
// The $filteredUsers variable will contain the following data:
// [
// ['name' => 'John Doe', 'age' => 30],
// ['name' => 'Bob Smith', 'age' => 40],
// ]
In this example, the filter()
method takes a closure as an argument that defines the condition for filtering the collection. The closure returns true
if the user's age is greater than 25 and false
otherwise. The filter()
method then returns a new collection containing only the users that match the condition.
Yes, you can update the filtered data and have the changes reflected in the original collection using the each()
method.
Here's an example of how you can achieve this:
$users = collect([
['name' => 'John Doe', 'age' => 30],
['name' => 'Jane Doe', 'age' => 20],
['name' => 'Bob Smith', 'age' => 40],
['name' => 'Alice Jones', 'age' => 25],
]);
$filteredUsers = $users->filter(function ($user) {
return $user['age'] > 25;
});
$filteredUsers->each(function (&$user) {
$user['is_old'] = true;
});
// The $users variable will now contain the following data:
// [
// ['name' => 'John Doe', 'age' => 30, 'is_old' => true],
// ['name' => 'Jane Doe', 'age' => 20],
// ['name' => 'Bob Smith', 'age' => 40, 'is_old' => true],
// ['name' => 'Alice Jones', 'age' => 25],
// ]
In this example, we first filter the users who are older than 25 using the filter()
method and store the result in the $filteredUsers
variable. We then use the each()
method to iterate over each element in $filteredUsers
and add a new key is_old
with the value true
. By passing the $user
argument with an ampersand (&
) in the each()
callback function, we are passing the user by reference, so any changes made to $user
will also be reflected in the original $users
collection.
If you are certain that your Laravel collection will only have one item, you can use the first()
method to retrieve that item.
Here's an example:
$collection = collect([['name' => 'John Doe'], ['name' => 'Jane Doe']]);
$item = $collection->first();
// $item will contain the following data:
// ['name' => 'John Doe']
In this example, we have a collection with two items. However, if you are certain that your collection will only have one item, you can use the first()
method to retrieve that item. The first()
method returns the first element of the collection that passes a given truth test. If no truth test is provided, it returns the first element of the collection.
So, in the above example, $item
will contain the first element of the collection, which is ['name' => 'John Doe']
.
You can count the elements in a Laravel collection based on object properties using the where()
method in combination with the count()
method.
Here's an example:
$users = collect([
['name' => 'John Doe', 'age' => 30],
['name' => 'Jane Doe', 'age' => 20],
['name' => 'Bob Smith', 'age' => 40],
['name' => 'Alice Jones', 'age' => 25],
]);
$count = $users->where('age', '>', 25)->count();
// $count will be 2
In this example, we have a collection of users. We use the
where()
method to filter the users who are older than 25 and then use thecount()
method to count the number of users who match the filter condition.
The where()
method takes a key-value pair as arguments and returns a new collection containing only the elements that match the given key-value pair. In this case, we pass 'age'
as the key and '>', 25
as the value to filter the users who are older than 25. Finally, we use the count()
method to count the number of elements in the filtered collection.
To query a Laravel collection using an OR condition, you can use the filter()
method with a closure that implements the OR condition.
Here's an example:
$users = collect([
['name' => 'John Doe', 'age' => 30, 'gender' => 'male'],
['name' => 'Jane Doe', 'age' => 20, 'gender' => 'female'],
['name' => 'Bob Smith', 'age' => 40, 'gender' => 'male'], ['name' => 'Alice Jones', 'age' => 25, 'gender' => 'female'],
]);
$filteredUsers = $users->filter(function ($user) {
return $user['age'] > 25 || $user['gender'] == 'female';
});
// The $filteredUsers variable will contain the following data:
// [
// ['name' => 'John Doe', 'age' => 30, 'gender' => 'male'],
// ['name' => 'Jane Doe', 'age' => 20, 'gender' => 'female'],
// ['name' => 'Bob Smith', 'age' => 40, 'gender' => 'male'],
// ['name' => 'Alice Jones', 'age' => 25, 'gender' => 'female'],
// ]
In this example, we have a collection of users with their respective ages and genders. We use the filter()
method to filter the users based on the OR condition that either their age is greater than 25 or their gender is female.
The filter()
method takes a closure as an argument that defines the condition for filtering the collection. The closure returns true
if the user matches any one of the OR conditions and false
otherwise. The filter()
method then returns a new collection containing only the users that match the condition.