Laravel Collection map() Add Attribute Example

By Hardik Savani April 16, 2024 Category : Laravel

Hello Dev,

In this tutorial, you will learn laravel collection map add attribute. if you want to see an example of laravel collection add attribute then you are in the right place. if you want to see an example of laravel collection append attribute then you are in the right place. This article will give you a simple example of how to add attribute in laravel collection map. Follow the below tutorial step of laravel collection map append attribute.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions.

Sometimes, we need to add custom attributes in the laravel eloquent collection object. For example, if we are getting posts with status and you need to set the label on status like "0" mean "InActive" and "1" means "Active", Then you can do it using the laravel collection map() function.

I will give you simple following three examples:

1) Laravel Eloquent Collection Add Attribute using setAttribute()

2) Laravel Collection Add Attribute using map()

3) Laravel Eloquent Collection Add Attribute using map()

Let's see the below examples:

Example 1: Laravel Eloquent Collection Add Attribute using setAttribute()

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$user = User::first();

$user->setAttribute('position', 'Web Developer');

dd($user->toArray());

}

}

Output:

array:8 [

"id" => 1

"name" => "Hardik Savani"

"email" => "aatmaninfotech@gmail.com"

"email_verified_at" => null

"created_at" => "2022-05-23T12:58:18.000000Z"

"updated_at" => "2022-05-23T12:58:18.000000Z"

"birthdate" => "2001-05-23"

"position" => "Web Developer"

]

Example 2: Laravel Collection Add Attribute using map()

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Str;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = collect([

['id'=>1,'title'=>'Post One'],

['id'=>2,'title'=>'Post Two'],

['id'=>3,'title'=>'Post Three'],

['id'=>4,'title'=>'Post Four'],

]);

$posts = $posts->map(function ($post) {

$post['url'] = url("post/". Str::slug($post['title']));

return $post;

});

dd($posts->toArray());

}

}

Output:

Array

(

[0] => Array

(

[id] => 1

[title] => Post One

[url] => http://localhost:8000/post/post-one

)

[1] => Array

(

[id] => 2

[title] => Post Two

[url] => http://localhost:8000/post/post-two

)

[2] => Array

(

[id] => 3

[title] => Post Three

[url] => http://localhost:8000/post/post-three

)

[3] => Array

(

[id] => 4

[title] => Post Four

[url] => http://localhost:8000/post/post-four

)

)

Example 3: Laravel Eloquent Collection Add Attribute using map()

app/Http/Controllers/DemoController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class DemoController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index(Request $request)

{

$posts = Post::select("id", "title", "status")->take(5)->get();

$posts = $posts->map(function ($post) {

$post->status_label = $post->status == 1 ? 'Active' : 'InActive';

return $post;

});

dd($posts->toArray());

}

}

Output:

Array

(

[0] => Array

(

[id] => 2

[title] => Demo Updated

[status] => 1

[status_label] => Active

)

[1] => Array

(

[id] => 3

[title] => Dr.

[status] => 0

[status_label] => InActive

)

[2] => Array

(

[id] => 4

[title] => Miss

[status] => 0

[status_label] => InActive

)

[3] => Array

(

[id] => 5

[title] => Dr.

[status] => 0

[status_label] => InActive

)

[4] => Array

(

[id] => 6

[title] => Mrs.

[status] => 0

[status_label] => InActive

)

)

I hope it can help you...

Tags :
Shares