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.
26 episodes2 hrs 34 mins

Overview

Need to know what’s new in Laravel as it happens? Every episode of this course is dedicated to covering the most interesting and useful Laravel additions in detail, so you’re ready to start using them in your applications.

Check back often, and stay completely up-to-date with Laravel.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!