DEV Community

Cover image for Collection In Laravel
Code Of Accuracy
Code Of Accuracy

Posted on

Collection In Laravel

A collection is a robust data structure that allows developers to work with arrays of data more easily and efficiently. It provides an object-oriented approach to working with arrays and offers a wide range of methods that can be used to manipulate, filter, sort, and transform data.

Here's an example of how to create a collection in Laravel:

$collection = collect(['apple', 'orange', 'banana', 'kiwi']);
Enter fullscreen mode Exit fullscreen mode

In this example, we have created a collection of fruits. Now we can use various methods to work with this collection. For example, we can use the filter method to filter out all the fruits that start with the letter a:

$filtered = $collection->filter(function ($value, $key) {
    return Str::startsWith($value, 'a');
});

// Output: ['apple']
Enter fullscreen mode Exit fullscreen mode

In this example, we have used the filter method to create a new collection that only contains the fruit apple, which is the only fruit that starts with the letter a.

We can also use the sort method to sort the fruits alphabetically:

$sorted = $collection->sort();

// Output: ['apple', 'banana', 'kiwi', 'orange']
Enter fullscreen mode Exit fullscreen mode

In this example, we have used the sort method to create a new collection that contains the fruits sorted alphabetically.

One of the main benefits of collections in Laravel is the ability to chain methods together, allowing developers to perform multiple operations on a collection in a single line of code. Here's an example of how we can create a collection of users and filter them by age and then sort them by name:

$users = collect([
    ['name' => 'John', 'age' => 30],
    ['name' => 'Jane', 'age' => 25],
    ['name' => 'Bob', 'age' => 35],
    ['name' => 'Sarah', 'age' => 28]
]);

$filteredUsers = $users
    ->where('age', '>', 25)
    ->sortBy('name');

// Output: [['name' => 'Jane', 'age' => 25], ['name' => 'John', 'age' => 30], ['name' => 'Sarah', 'age' => 28], ['name' => 'Bob', 'age' => 35]]
Enter fullscreen mode Exit fullscreen mode

In this example, we have created a collection of users with their names and ages. We then used the where method to filter the collection, only returning users who are older than 25. Finally, we used the sortBy method to sort the filtered collection by name.

Another useful method of collections in Laravel is the map method. This method allows developers to transform the values in a collection by applying a callback function to each item in the collection. Here's an example of how we can use the map method to transform a collection of users into a collection of their full names:

$users = collect([
    ['first_name' => 'John', 'last_name' => 'Doe'],
    ['first_name' => 'Jane', 'last_name' => 'Smith'],
    ['first_name' => 'Bob', 'last_name' => 'Johnson']
]);

$fullNames = $users->map(function ($user) {
    return $user['first_name'] . ' ' . $user['last_name'];
});

// Output: ['John Doe', 'Jane Smith', 'Bob Johnson']
Enter fullscreen mode Exit fullscreen mode

In this example, we have used the map method to create a new collection of full names by concatenating the first name and last name of each user in the original collection.

Top comments (0)