What is Laravel Collection? I will explain it in an easy-to-understand way
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
⇨ Click here for the table of contents for Laravel articles
I will explain about Laravel collections. For beginners, I think you might be wondering "What even is a collection?", so I hope this explanation helps.
The content of this article is based on excerpts from Laravel official documentation.
What is a Collection?
Roughly speaking, it's like an array.
Intuitively, it can be divided into the following categories:
① What you get when you use get() or all() methods for data retrieval
② Things you generate using collect yourself
① Creating a Collection with a Simple Array
When no key is specified, it will be displayed in the following format.
$array = [1,2,3,4,5];
$collection1 = collect($array);
dd($collection1);
Illuminate\Support\Collection {#338 ▼
#items: array:5 [▼
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
]
}
If you specify the array index, you can retrieve only that element.
dd($collection1[0]);
// Outputs 1
② Creating a Collection with a key
Unlike before, you will use a key.
$array2 = [
[
'user_id'=> 1,
'name' => 'Sato',
],
[
'user_id'=> 2,
'name' => 'Takeru',
],
[
'user_id'=> 3,
'name' => 'Shun',
],
[
'user_id'=> 4,
'name' => 'Oguri',
],
];
$collection2 = collect($array2);
dd($collection2);
Output
Illuminate\Support\Collection {#350 ▼
#items: array:4 [▼
0 => array:2 [▼
"user_id" => 1
"name" => "Sato"
]
1 => array:2 [▼
"user_id" => 2
"name" => "Takeru"
]
2 => array:2 [▼
"user_id" => 3
"name" => "Shun"
]
3 => array:2 [▼
"user_id" => 4
"name" => "Oguri"
]
]
}
Specify the order just like with arrays.
dd($collection2[0]);
Output
// Only the first data is retrieved.
array:2 [▼
"user_id" => 1
"name" => "Sato"
]
To retrieve a specific key
dd($collection2[0]['user_id']);
Output
// Only the user_id of the first element is retrieved
1
Benefits
It's easy to manipulate data, such as searching or changing the order.
Example
I think I created an example using key earlier,
Let's say we want to retrieve the data where user_id is 1.
//You can search by key using where
dd($collection2->where('user_id',1));
Output
// Retrieved the data where user_id is 1.
// If there are multiple data with user_id 1, they will all be retrieved. Therefore, the retrieved data is a collection.
Illuminate\Support\Collection {#353 ▼
#items: array:1 [▼
0 => array:2 [▼
"user_id" => 1
"name" => "Sato"
]
]
}
These searching methods like where
are quite similar to the ones used in Eloquent when retrieving data from a database (like where and orderBy), so they are easy to understand.
Are Collections used when retrieving data from a database?
A. Collections are used for retrieving multiple items.
(Single item retrieval results in an object)
In other words,
Post::get();
Post::all();
will retrieve multiple items as a Collection.
A. Single item retrieval results in an object.
Post::first();
Post::find(1);
will retrieve a single item as an object.
Summary
That's all.
Personally, I thought it might be easier to understand if I explained what a collection is by showing examples rather than explaining in words, so I wrote this.
I would be glad if it was helpful to someone!
If you have any comments or feedback, please contact me via DM on Twitter.
That's all!
Popular Articles
Deploying a PHP 7.4 + Laravel 6 Project to AWS EC2
Implementing breadcrumbs in Laravel using laravel-breadcrumbs