Laravel Map() : Laravel Collection Method with Example

Laravel Map() : Laravel Collection Method with Example

Laravel map() is a Laravel Collections method to pass callback function to each item in Collection. It is a easy yet powerful method to manipulate data in Laravel Collection. After operating on each item, map() method returns new Collection object with updated items.

Laravel map() is a Laravel Collections method to pass callback function to each item in Collection. It is a easy yet powerful method to manipulate data in Laravel Collection. After operating on each item, map() method returns new Collection object with updated items.

Note: The map() method will not modify the original Laravel Collection, so it is safe to use without worrying about unknown data mutation.

The Callback function requires two parameters - $item$key in this order. Obviously these are just variables to hold parameter value, so you can use any name.

Definition

Here is the actual definition of map() method defined in Illuminate\Support\Collection class. This is just for understanding, you won't have to worry about this.

/**
 * Run a map over each of the items.
 *
 * @param  callable  $callback
 * @return static
 */
public function map(callable $callback)
{
    $keys = array_keys($this->items);

    $items = array_map($callback, $this->items, $keys);

    return new static(array_combine($keys, $items));
}

Laravel map() Example with Generic Collection

Here's the simple example:

$names = collect(["john","dave","michael"]);

//let's try changing first letter to uppercase in all names

$ucnames = $names->map(function($name, $key) {
    return ucwords($name);
});

$ucnames->all();

// ["John","Dave","Michael"]

Laravel map() Example with Eloquent Collection

map() method is often used to manipulate data fetched from Eloquent queries. So, let's review one such example:

$users = User::all();

$mappedcollection = $users->map(function($user, $key) {                                    
    return [
            'id' => $user->id,
            'name' => $user->name
        ];
    });

//output

[
    [
        "id" => 1,
        "name" => "John Admin"
    ],
    [
        "id" => 2,
        "name" => "John Doe"
    ]
]

laravel

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Php how to delete multiple rows through checkbox using ajax in laravel

In this article i will let you know to delete multiple rows through checkbox using ajax in laravel and before delete we will give a confirmation message.

How to Hire Laravel Developers and How Much Does It Cost.

Want to make the most out of the latest Laravel technology? Here is your guide on how to hire the best Laravel developer, and at the right price. 

How to perform migration rollback in laravel

Sometimes we need to rollback one specific migration. Here, in this article we will discuss about the migration rollback in laravel.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

5 Laravel’s Hidden Gems

Spread the love1. Stop on first validation error By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve…Read More→