1// The array we're going to return
2 $data = [];
3// Query the users table
4$query = users::where('id', 1)->get();
5
6// Let's Map the results from [$query]
7$map = $query->map(
8 function($items){
9 $data['user_firstName'] = $items->firstName;
10 $data['user_lastName'] = $items->lastName;
11 return $data;
12 }
13 );
14
15return $map;
1$collection = collect([1, 2, 3, 4, 5]);
2
3$multiplied = $collection->map(function ($item, $key) {
4 return $item * 2;
5});
6
7$multiplied->all();
8
9// [2, 4, 6, 8, 10]
1$collection = collect([
2 [
3 'name' => 'John',
4 'department' => 'Sales',
5 'email' => 'john@example.com'
6 ],
7 [
8 'name' => 'Jane',
9 'department' => 'Marketing',
10 'email' => 'jane@example.com'
11 ]
12]);
13
14$keyed = $collection->mapWithKeys(function ($item) {
15 return [$item['email'] => $item['name']];
16});
17
18$keyed->all();
19
20/*
21 [
22 'john@example.com' => 'John',
23 'jane@example.com' => 'Jane',
24 ]
25*/
1// The array we're going to return$data = [];// Query the users table$query = users::where('id', 1)->get();// Let's Map the results from [$query]$map = $query->map(function($items){ $data['user_firstName'] = $items->firstName; $data['user_lastName] = $items->lastName; return $data;});return $map;