map() Method | Laravel Collection Example

  • 14-05-2022
  • 1227
  • Laravel 9
  • Haresh Chauhan

Laravel map() collection return new collection from the given collection, map collection make the data return callback with different way and can various the that data in different variation.

In this example, you will learn how to use the map() function in laravel collection and how to make collections in different variations.

map() also you can use in eloquent in laravel model collection.

Here below given some working examples with different use of the map() function in laravel.

Example 1: Use simple example

In this example, I made each item multiple with ten time. you can see output example using map function that item result output ten times of each item.

$data = [
    10,
    20,
    30,
    40,
    50
];

$collection = collect($data)->map(function($item){
    return $item * 10; 
});

// OUPPUT 

Illuminate\Support\Collection {#262 ▼
  #items: array:5 [▼
    0 => 100
    1 => 200
    2 => 300
    3 => 400
    4 => 500
  ]
}

Example 2: Use with key(index)

In this example used the index of collection, each item of collection multiple with each item index key.

$data = [
    10,
    20,
    30,
    40,
    50
];

$collection = collect($data)->map(function($item,$key){
    return $item * $key; 
});

// OUTPUT

Illuminate\Support\Collection {#264 ▼
  #items: array:5 [▼
    0 => 0
    1 => 20
    2 => 60
    3 => 120
    4 => 200
  ]
}

Example 3: Use maping with concat

In this example, the map function is used with concat each item value concat each item key.

$data = [
    'key1' => 'value1',
    'key2' => 'value2',
    'key3' => 'value3',
    'key4' => 'value4',
    'key5' => 'value5',
];

$collection = collect($data)->map(function($item,$key){
    return $item .' '. $key; 
});

// OUTPUT

Illuminate\Support\Collection {#264 ▼
  #items: array:5 [▼
    "key1" => "value1 key1"
    "key2" => "value2 key2"
    "key3" => "value3 key3"
    "key4" => "value4 key4"
    "key5" => "value5 key5"
  ]
}

Example 4: Use with callback different collection

This example map method will return a new collection with multiple dimensional arrays.

$data = [
  10,
  20,
];

$collection = collect($data)->map(function($item,$key){
  return [
    'key' => $key,
    'value' => $item,
    'divide' => $item / ($key + 1),
    'multiple' => $item * $key,
    'array' => [$item => $key]
  ]; 
});

// OUTPUT

Illuminate\Support\Collection {#264 ▼
  #items: array:2 [▼
    0 => array:5 [▼
      "key" => 0
      "value" => 10
      "divide" => 10
      "multiple" => 0
      "array" => array:1 [▼
        10 => 0
      ]
    ]
    1 => array:5 [▼
      "key" => 1
      "value" => 20
      "divide" => 10
      "multiple" => 20
      "array" => array:1 [▶]
    ]
  ]
}

Note: The map collection method never skips any item of the collection, in case you did not return any callback also it will take auto result null but the map collection method never skips collection item in a callback.

Even if you did not return any callback then it will take an auto null value in each item collection value.

Another one thinks to keep in mind is that maps never return the same collection, map() method always return a new collection. and always use with callback function.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :