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([
2 ['product' => 'Desk', 'price' => 200],
3 ['product' => 'Chair', 'price' => 100],
4 ['product' => 'Bookcase', 'price' => 150],
5 ['product' => 'Door', 'price' => 100],
6]);
7
8$filtered = $collection->whereIn('price', [150, 200]);
9
10$filtered->all();
11
12/*
13 [
14 ['product' => 'Bookcase', 'price' => 150],
15 ['product' => 'Desk', 'price' => 200],
16 ]
17*/
1$collection = collect([1,2,3,4]);
2
3$collection->each(function($item){
4 return $item*$item;
5});
6
7// [1,4,9,16]
1$collection = collect([1, 2, 3]);
2
3$collection->when(true, function ($collection) {
4 return $collection->push(4);
5});
6
7$collection->all();
8
9// [1, 2, 3, 4]
1$collection = collect(['name' => 'taylor', 'framework' => 'laravel']);
2
3$value = $collection->get('name');
4
5// taylor
1$collection = collect([
2 ['product' => 'Desk', 'price' => 200],
3 ['product' => 'Chair', 'price' => 100],
4 ['product' => 'Bookcase', 'price' => 150],
5 ['product' => 'Door', 'price' => 100],
6]);
7
8$filtered = $collection->where('price', 100);
9
10$filtered->all();
11
12/*
13 [
14 ['product' => 'Chair', 'price' => 100],
15 ['product' => 'Door', 'price' => 100],
16 ]
17*/