Playing
12. CollectedBy Attribute

Transcript

00:00
If you're using custom collections in your application you will love this change. It's a new collected by attribute. Even if you don't let's run through the process of creating a custom collection for a set of database results and see what this looks like. Okay so the first thing I'm going to do is go ahead and just dump out all of the users in my application here just using user
00:21
get. Let's go ahead and create some in the database just so we have some results to play with. So let's go ahead and use our user factory and we'll go ahead and create our 10 items in the database here. Okay so now that we've done that let's go over to the browser and have a look. Now this gets bundled up, these results get bundled up in a Laravel collection. So this is
00:42
just a plain old collection class which has all the methods you would expect to be able to do what you want with the data inside of these. It doesn't have to be for Eloquent, it can be just a standard collection as well but we're talking about model collections here that we get the results back from Eloquent. Now to create a custom collection, first of all why would you want to create a custom
01:04
collection? Well you might have very specific functionality you want to add onto this collection. There are multiple reasons but let's take a look at how this works now. So somewhere in our app let's go ahead and create out a new directory. So let's create a directory out in here called collections. You could of course put these anywhere. Let's go ahead and create out a PHP
01:24
class called user collection. Now when we create this we could do this manually but we would want this to extend the base Illuminate support collection or database Eloquent collection. Both of these are pretty much the same thing. So let's go ahead and create that class out now and you can see that we've got now got a user collection. This extends the base collection
01:45
and what we can do is we can specify within the user model, so let's open this up, that when we get back a collection of these items with Eloquent, which collection do we want to use? Now by the default that's the standard collection. Let's go down to the bottom of our class and let's define out a public function in here called new collection. This just overrides what we already have
02:08
so we can even just bring that in with the signature from the base model. So this gives us an array of models so there'll be an array of users and we can do whatever we want in here. So we could return a new user collection now and we could hydrate that collection with the models that we get back from the database. So now that we've done this, now that we've defined this new
02:30
collection method, what's going to happen is we are now getting back a user collection which is going to work in exactly the same way as a standard collection because we're just extending it but we now have a custom collection. That means that over on the user collection we could define out any of our own methods in here to do something with the data inside of it. Now that's beyond the
02:51
scope of what we're talking about here, we just want to know what this new collected by attribute is and how it works. Well it's a lot simpler so over in our model rather than defining or overriding this new collection method we can do something a lot more convenient. We can come right to the top of our model and we can use the collected by attribute specifying the name of the
03:17
collection. That is our user collection, let's go ahead and pull the full namespace of that into here and that is going to have exactly the same effect and you can see we've still got a custom user collection despite the fact we haven't defined out that method or overrided that method. So a much easier way now to define a custom collection for any of our models when we need them.

Episode summary

In this episode, we're diving into a really handy new feature for Laravel developers: the collectedBy attribute. If you've worked with custom collections in your Laravel apps, this is going to make your life a lot easier! Even if you haven't created custom collections before, we'll walk through the process together.

First, we start off by generating some sample data with a user factory so we have some users in the database to play with. Then, I show how Eloquent returns your database results as a standard collection class, which gives you all those convenient collection methods to work with your data.

Next up, I explain why you might want a custom collection—maybe to add extra methods or special behavior to the group of models you pull from the database. We create a new UserCollection class to demonstrate this, extending the base collection class. Traditionally, to tell Laravel to use your custom collection, you'd override the newCollection method in your model.

But here's the exciting part: with Laravel's new collectedBy attribute, you don't have to go through the hassle of overriding methods. Instead, you just add one attribute at the top of your model specifying your custom collection class, and Laravel takes care of the rest.

So in short: custom collections are now way simpler to use—just set the collectedBy attribute on your model and you're good to go! Less boilerplate, more convenience.

Episode discussion

No comments, yet. Be the first!