Introduction to Collections 0:00Let's talk about collections. I'm going to boot up PHP Artisan Tinker. Now as you know, let's fetch all users in our system. So if we run this command, you'll see that we do receive a collection of users. Think of a collection as an array on steroids. So if we do have an array of about four items in this case, the collection is the container or the wrapper around it, and it gives us a nice API for interacting with it, which means if in the past you're always researching PHP array functions and it sometimes can get a little frustrating because the argument order is inconsistent, honestly with Laravel and collections, you don't even have to think about it. Basic Collection Retrieval 0:36a little frustrating because the argument order is inconsistent, honestly with Laravel and collections, you don't even have to think about it. So for example, let's fetch all of our users again. And yes, I can still interact with it as if it was an array. So I could say give me the first one. However, because this is a collection, I could instead make it more readable. Give me the first one, or give me the last one, or let's do this again. Give me the user with an ID of four. Okay, users, find the one where the ID is four. So again, notice this looks like a database query, but it's not.Okay, users, find the one where the ID is four. So again, notice this looks like a database query, but it's not. It is a method on your collection class. Let's switch to Sublime. I'm going to go to illuminate support collection. This will be in your vendor directory. Here's the full path. Now you'll see if I view the methods, here are all the various methods that you can use. Lots of stuff here, like each, or every, or what else, where. So again, notice that the API is very similar to how you might query your database withLots of stuff here, like each, or every, or what else, where. So again, notice that the API is very similar to how you might query your database with Eloquent. So let's try this one out. Users, well, let's say we want to find this record again, but we only have the email. Okay. So let's say users where the email address is equal to this, and give me the result. Okay. So now I have a collection of all users with that email, but that's not quite what I want. I want the exact user record. Pluck and Array Conversion 1:57So now I have a collection of all users with that email, but that's not quite what I want. I want the exact user record. So I'll say give me the first one. Okay. So now I have the user object, at which point I can grab their name. Let's do another one. All of this is available through the collection object. So let's say I just want to grab all of the email addresses for this collection. Okay. So please pluck for that.Okay. So please pluck for that. Users, pluck the email address. So now you'll see it's going to return not an array, but once again, a new collection that contains only these items. So if I want to say give me the very first one, there you go. Or sometimes you do have a collection and you want to say, okay, I now need to convert this to just a basic vanilla array. Okay. At this point, you could say convert that to an array, and now we have that.Okay. At this point, you could say convert that to an array, and now we have that. Now another way to do this, we can use pluck, and that would be the right choice. But you can also use map. So many of the standard PHP array functions are still available through collections. So for example, maybe I wanted to grab the name here. Well, yes, I would recommend using pluck. But I could also say users, and I'm going to map over every single user, and I'm going to return just the user's name in this case. Okay. Mapping and Filtering Items 3:13to return just the user's name in this case. Okay. And now I'm going to get the exact same thing. So this map method, it's just array map. It's a wrapper around that to make it really easy for you. And if you're not familiar with mapping, think of it as a way of saying, okay, I want to map over every single item in this array, and I want to convert that item into whatever I return here. So for example, if I do this, and for each one I say foobar, so for each item in the users array, I just want to, I'm sorry, I just want to return foobar.So for example, if I do this, and for each one I say foobar, so for each item in the users array, I just want to, I'm sorry, I just want to return foobar. There we go. All right. Now you'll have a collection where every single item is equal to whatever you returned there. So that can be really useful. Another option, if we bring back users, is filter. Now we don't really have much here that we can filter by. Why don't we just do it according to the ID? But yeah, you might want to filter according to those who have an active subscription orWhy don't we just do it according to the ID? But yeah, you might want to filter according to those who have an active subscription or those who have canceled or those who have paid their invoice or things like that. Okay. Let's say users, and we're going to use filter here. I like to think of filter literally as a filter, a little container there. And we're going to add a function here that specifies whether or not each item in the collection can make it through to the other side. So we'll say that determining factor will be if the user's ID is greater than or equal to 3.So we'll say that determining factor will be if the user's ID is greater than or equal to 3. Okay. So now you'll see we get a new collection, but only consisting of the users that have an ID of 3 or higher. Or if we want to do the opposite. Okay. Now we have only the users with an ID of 3 or less. Now in addition to working with the results of an Eloquent query, you can also use collections on your own. Using Collect on Arrays 4:52Now in addition to working with the results of an Eloquent query, you can also use collections on your own. And we offer this helpful collect function. So let's say collect foo bar baz. And you'll see we've given it an array, and that collect function wraps it within a collection instance. So imagine you have this little array here, and we'll go ahead and save that. So let's say we want to capitalize all of these. Okay. Well, traditionally, you might say something like for each, or you might even use the arrayOkay. Well, traditionally, you might say something like for each, or you might even use the array map function. But even this is hard sometimes because you forget the order. I mean, how many times have you done that? I do it myself all the time. So anyways, we'll map over it, and we'll say return string to upper, and then pass in the item there. And then finally, we'll give it the array of items. Okay.And then finally, we'll give it the array of items. Okay. So now you have a new array where each item has been capitalized. We could instead say collect those items. All right. Now we have a collection instance. So we could now, we could really do this in a bunch of ways, but to reproduce that, map over each of the items, and return string to upper, like so. Okay. What else? Summing Song Lengths 6:04Okay. What else? There's honestly so many things I could show you here. But to fit this into about 15 minutes, let's do one more here. Let's say you have a bunch of songs here. Now in real life, you might have a song class, but I'm just going to use a standard PHP object. So we'll say, give me an array, and then we'll map it to an object where the name is something, and the length in seconds is 300. Okay. Next, let's do a song 2, and this will be foobar with another length of 200.Okay. Next, let's do a song 2, and this will be foobar with another length of 200. And then finally, song 3, and this will be yet another song. Okay. So now I have three songs at this point that I can now collect into an array, or I'm sorry, collect into a collection instance. So yes, we've learned, if we save this, yes, we've learned that we can do things like pluck the name of each song, and now we have a new array. And this is very useful for your views, because you can then say, for each one of those, or each, then spit it out in a list item or something like that.And this is very useful for your views, because you can then say, for each one of those, or each, then spit it out in a list item or something like that. We could also get the length. So let's say, so we could say songs, and then grab the length. But now what if you're on maybe an album page, and you need to get the total length of all of those songs? Well once again, you might do something like you start length at zero, and then you say for each songs as song, we'll say length plus equals song dot length. I bet you've done stuff like this before. Okay.I bet you've done stuff like this before. Okay. So now if we spit out length, whoops, anyways, we now have 900, which is the total length of the album. But if we take a more collection approach, what might we do instead? Well, we want to get the sum of all of those items, right? So how about this? Songs, and we're going to call a method sum. Done. Now we get the exact same thing. Higher Order Collections 8:01Done. Now we get the exact same thing. Now final thing, higher order collections. I think we touched on this a bit many lessons ago. Let's go ahead and find all of our users one more time. Higher order collections give us sort of a streamlined syntax and API. So for example, let's say we want to grab all users who, hmm, we'll say who are verified. So at the moment it looks like all of my emails are verified. Let's give this a refresh, and I'm going to update some of these. And we'll say this one is not, and this one is not.Let's give this a refresh, and I'm going to update some of these. And we'll say this one is not, and this one is not. Okay. So now if I run it again, you'll see it looks like we have exactly two users who are not verified. So imagine you need to fetch all users who are not verified or who are verified. Okay. Well, you might do this. You have your users collection, right? You might say users, filter it down to where the current user is verified, and we willYou have your users collection, right? You might say users, filter it down to where the current user is verified, and we will return that. So if that's a truthy value, it gets included in the results, and if it's falsy, it will not be included. Okay. So now you'll see we get a new collection consisting of only the users who are verified. Or if we want to do the opposite, you might say not. Okay. So now we get only the users who have not yet been verified.Okay. So now we get only the users who have not yet been verified. But let me show you how we can streamline this a bit more using a higher order collection. What if I said users, filter it down to only the users who have a email verified at column that is not null. Okay. Now we get the exact same thing. This is very cool. So what's happening here? Well, behind the scenes, Laravel is figuring out you're calling some property or methodSo what's happening here? Well, behind the scenes, Laravel is figuring out you're calling some property or method that doesn't exist on the main collection instance. So if we scroll down, you'll see, all right, in that situation, this method will be called dynamically access collection proxies, and you'll see that it returns this higher order collection. This is all fancy jargon. You don't even really need to worry about it, just accept that when you call something like this or you call a method, Laravel will assume, oh, I bet you want to call that on each item of the collection.like this or you call a method, Laravel will assume, oh, I bet you want to call that on each item of the collection. So this would be identical to saying users, filter it down to where the users email verified at property is truly in this case. Another way to do it is through a method. So let's go to our user class, and if I scroll down, we'll say, let's add a method called is verified, and all that's going to return is this email verified at, and maybe even make it a Boolean or like that. Okay. So now, take a look at this.Okay. So now, take a look at this. I'm going to boot up PHP Artisan Tinker. We'll find our users again. And yes, for each user, so I could say, give me the first user and let me know if they are verified. This person is, so we get true. But now on that collection, I can filter it down, and let's do it the long way and then the higher order collections way. Anyways, filter that down to where the current user is verified.the higher order collections way. Anyways, filter that down to where the current user is verified. Okay. So now we get a new collection, once again, of only the users who are verified. So we can do it like that, or we could do it like this. Users, filter it down, so we now have an implicit user, and we'll check is verified. All right, now we get the same thing. You'd even do the opposite here. So maybe you want to have one that is not verified. And all this has to do is return not.So maybe you want to have one that is not verified. And all this has to do is return not. This is verified. Okay, now we have two methods for determining if a user is or is not verified. So we'll run it one more time, and we'll say, filter my collection down to only the users who are verified or not verified. And in this case, we get a collection of two items where email verified at is null. All right, so admittedly, we went pretty quick there. It was mostly meant to whet your appetite and show you all of the flexibility you have. But just rest assured, when building a Laravel app, you will be working with collections Custom Eloquent Collections 12:16It was mostly meant to whet your appetite and show you all of the flexibility you have. But just rest assured, when building a Laravel app, you will be working with collections constantly. You will either manipulate collections that Eloquent returns to, you can create your own generic collections on the fly, or you can even create and extend your own custom collections, which would allow you to do things like this. If you have your users, you'll see at the moment it is just a standard Illuminate database Eloquent collection. But maybe it's your own app user collection. Then you could say, well, I want to use specific domain language from my project alone.But maybe it's your own app user collection. Then you could say, well, I want to use specific domain language from my project alone. Maybe give me all of the users who are all verified. Okay, now we introduce a second level of readability. And should I show you that? No. Rather than that, you can review it on your own. Research Eloquent custom collections. All you need to do is add an extra method to your user class and then create a new users collection class that it will return.All you need to do is add an extra method to your user class and then create a new users collection class that it will return. So I do recommend researching that on your own. And if you need any help, you can ask away in the comments below.