Laravel Collection Map Method Example

Laravel Collection Map Method Example

In this post we will give you information about Laravel Collection Map Method Example. Hear we will give you detail about Laravel Collection Map Method ExampleAnd how to use it also give you demo for it if it is necessary.

Are you looking for example of laravel collection map example. i explained simply about laravel eloquent collection map. We will use laravel collection map. This article goes in detailed on laravel map example.

The map method will given callback function to each element of a collection. The callback function will modify a item and create a new Laravel collection.

I will give you simple examples of map colletion in laravel. so you can easily use it with your laravel 5, laravel 6 and laravel 7 application. so let’s see bellow example that will helps you lot.

Syntax:

$collecton->map(

Callback

);

Laravel Collection map() Example

public function index()

{

$collection = collect(["one", "two", "three", "four", "five"]);

$modified = $collection->map(function($item, $key) {

return strtoupper($item);

});

dd($modified);

}

Output:

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[0] => ONE

[1] => TWO

[2] => THREE

[3] => FOUR

[4] => FIVE

)

)

Laravel Eloquent Collection Map

If you need to change column name for your api response or front end side you can easily use map method for change that as like bellow:

public function index()

{

$collection = User::get();

$modified = $collection->map(function($item, $key) {

return [

'id' => $item->id,

'name' => $item->name,

'creation_date' => $item->created_at->format('m/d/Y')

];

});

dd($modified);

}

Output:

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 1

[name] => Hardik

[creation_date] => 04/19/2020

)

[1] => Array

(

[id] => 2

[name] => HD Surgon

[creation_date] => 03/28/2020

)

)

)

I hope it can help you…

Hope this code and post will helped you for implement Laravel Collection Map Method Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  74  =  81

Laravel Collection Map Method Example

Laravel Collection Map Method Example

In this post we will give you information about Laravel Collection Map Method Example. Hear we will give you detail about Laravel Collection Map Method ExampleAnd how to use it also give you demo for it if it is necessary.

Are you looking for example of laravel collection map example. i explained simply about laravel eloquent collection map. We will use laravel collection map. This article goes in detailed on laravel map example.

The map method will given callback function to each element of a collection. The callback function will modify a item and create a new Laravel collection.

I will give you simple examples of map colletion in laravel. so you can easily use it with your laravel 5, laravel 6 and laravel 7 application. so let’s see bellow example that will helps you lot.

Syntax:

$collecton->map(

Callback

);

Laravel Collection map() Example

public function index()

{

$collection = collect(["one", "two", "three", "four", "five"]);

$modified = $collection->map(function($item, $key) {

return strtoupper($item);

});

dd($modified);

}

Output:

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[0] => ONE

[1] => TWO

[2] => THREE

[3] => FOUR

[4] => FIVE

)

)

Laravel Eloquent Collection Map

If you need to change column name for your api response or front end side you can easily use map method for change that as like bellow:

public function index()

{

$collection = User::get();

$modified = $collection->map(function($item, $key) {

return [

'id' => $item->id,

'name' => $item->name,

'creation_date' => $item->created_at->format('m/d/Y')

];

});

dd($modified);

}

Output:

IlluminateSupportCollection Object

(

[items:protected] => Array

(

[0] => Array

(

[id] => 1

[name] => Hardik

[creation_date] => 04/19/2020

)

[1] => Array

(

[id] => 2

[name] => HD Surgon

[creation_date] => 03/28/2020

)

)

)

I hope it can help you…

Hope this code and post will helped you for implement Laravel Collection Map Method Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  84  =  88

We accept paid advance and paid guest Posting on our Site : Contact US