Skip to main content

Command Palette

Search for a command to run...

Define collections in Laravel

Updated
1 min read
Define collections in Laravel
R

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, MySQL, MongoDB. Love collaborating & making a difference

Collections provide a convenient way to work with arrays of data. Laravel collections are an extension of the PHP array object, but they come with a variety of powerful methods that make manipulating and interacting with arrays more expressive and readable.

Allows you to chain its methods to perform fluent mapping and reducing of the underlying array. Collections are immutable, meaning every Collection method returns an entirely new Collection instance.

You can perform various operations on collections using the available methods. Some common methods include map, filter, each, pluck, count, sum, avg, max, min, etc.

$collection = collect(['taylor', 'abigail', null])->map(function (string $name) {
    return strtoupper($name);
})->reject(function (string $name) {
    return empty($name);
});

More from this blog

R

Raja Muhammad Asher - Senior Software Engineer - Full Stack Developer

157 posts

Full Stack Developer with a passion for building web applications. PHP, Node.js, Laravel, ExpressJS, MySQL, MongoDB. Love collaborating & making a difference