Intro to Collections 0:00Let's move on to some core concepts in Laravel. First up is Collections. So I'm going to boot up PHP Artisan Tinker, and I usually do it directly from the terminal, but of course, if you're working along in PHP Storm, you can also do it from Vue, Tool Windows, Terminal. And you can even expand it, resize, to full screen. All right, so this would work to whatever you want. Now I will begin by fetching the first article in our database. So notice, because we're only fetching one article, in response we get an article instance, which means I can access any of those attributes immediately.So notice, because we're only fetching one article, in response we get an article instance, which means I can access any of those attributes immediately. However, if you were to instead fetch all or many articles, in this case, what do we have, about a dozen different items there? Well, of course, in that case, you're not going to get an article in response, you will get a collection of articles. And what's great about this is, it's not just a simple wrapper around a series of items. You also get a huge number of affordances. So let's do this. Let's do another one. Collections from Relationships 1:04So let's do this. Let's do another one. We're going to find that article, once again, the first one, and then we're going to grab the tags relationship. So yet again, we get a collection. And you can always expect this when you have a has many relationship, or a many to many relationship. Okay, but anyways, let's play around with this. So I now have my tags variable. We could, of course, do simple things like give me the very first tag in the collection.So I now have my tags variable. We could, of course, do simple things like give me the very first tag in the collection. There it is. Or give me the last one. Or we can even interact with our tags, almost as if we were building a SQL query. But we're not. It's just a similar API. So we have our tags collection, but give me the one, filter it down, to only the one where the name is Laravel. There it is.the name is Laravel. There it is. So again, it's really important to understand, when we're calling where, it's not the same thing as doing this, even though we get the same result. In this case, we are building up an SQL query. But in this case, there's no database query being performed. It's all being done on a collection instance. Okay, so let's take a look at it together. So let's look for our collection class. And there it is. Exploring Collection Methods 2:14So let's look for our collection class. And there it is. So if we take a look at all the public methods here, yeah, look at all this stuff you get for free. And these are really common actions you'll need to do as part of any application. So yes, you have your first method. Get the first item from a collection. But notice it also accepts a optional callback. Okay, let's play around with that. So we'll say, once again, we have our tags collection.Okay, let's play around with that. So we'll say, once again, we have our tags collection. Let's do it again and say, all right, give me the first one, where the tag, and how about something simple like where the length of the tag's name is greater than five. Just something to toy around with. And once again, we get Laravel. Let's do the opposite. Give me one where it's less than four. Okay, it returns the first item in the collection where this closure returns true. Okay, let's keep going.Okay, it returns the first item in the collection where this closure returns true. Okay, let's keep going. So yes, once again, we have last. We've reviewed these. There's a where method. But then, of course, you also have things like flatten. And that'll flatten a nested array down to a single one. So for example, and in fact, I'll show you how to create a collection from scratch. You can use this collect helper function. So if I were to say collect one, two, three, we now have a collection of three items.You can use this collect helper function. So if I were to say collect one, two, three, we now have a collection of three items. So there you could say, again, give me the first one. But anyways, if we do it again, but we add a nested array. So this will be four, five, and then six. So again, notice we have some nested arrays here. Let's go ahead and flatten it all down to a single level. And there you go. So again, these are the sorts of things you'll reach for all the time for any project. And what's great is so often, you kind of have to refresh your memory.So again, these are the sorts of things you'll reach for all the time for any project. And what's great is so often, you kind of have to refresh your memory. Well, how do I do that again? And then you figure out some stack overflow thread with a bunch of PHP code. But instead, through Laravel collections, we don't even have to think about it. Okay, so let's look at a couple more. Now again, a quick note, even though there's dozens and dozens of methods here, like anything in your day to day, there's probably five to 10 that you'll reach for 80% of the time. And for me, those usually end up being some mixture of filter, or map, or flat map, or where, or merge. Filtering and Mapping 4:22And for me, those usually end up being some mixture of filter, or map, or flat map, or where, or merge. So again, these are kind of the more common things. But of course, you can move up and learn about zipping collections, which is neat, but sometimes does take a little time to understand the correct use case for it. Okay, but anyways, let's take a look at filter. So this allows us to filter a collection down to a subset of items. So if we were to try that out, we'll say items, and once again, we'll collect an array, and we'll just do a bunch of items here. Okay, so let's filter the items down to whatever we want.we'll just do a bunch of items here. Okay, so let's filter the items down to whatever we want. Let's start with something easy. We'll say filter the items down to only the ones that are greater than or equal to five. Okay, now we get a brand new collection of only the items that pass this test. But now remember, it's not destructive. So even though we filtered it down, it didn't rewrite the original variable. So if we run that, we still get the same item, which means if you want to filter it, you have to save it. Like so.have to save it. Like so. Let's do another one. How about give us the only items that are divisible by two, maybe with no remainder. Okay, well, you may know we can use the modulus operator for that. So if the item divided by two, if there is no remainder after that, so a remainder of zero in this case, then that should return true and be included in the result set. And of course, we get 2, 4, 6, 8, and 10. Okay, but now notice again, when we run this filter method, we get a collection in response, which means all of these methods are perfectly chainable.Okay, but now notice again, when we run this filter method, we get a collection in response, which means all of these methods are perfectly chainable. Like this. So let's run it again, but this time, we're going to continue chaining, and that's how we often refer to it. You are chaining collection calls. So after we filter the items down, we're going to map over them, and for each one, do whatever you want. How about we alter it in some way? Maybe return the item times three.How about we alter it in some way? Maybe return the item times three. Okay, so now you can see how we manipulate an existing collection. So we started with one through 10. This filter method filtered the items almost like they're going through an actual filter, but only the ones who make it through are the ones that pass this condition. So in that case, it would be 2, 4, 6, 8, and 10. Then we continued, and we mapped over that new collection, and for each one, we multiplied it by two. So 2 times 3 is 6, 4 times 3 is 12, 6 times 3 is 18. Eager Loading for Grouping 7:02it by two. So 2 times 3 is 6, 4 times 3 is 12, 6 times 3 is 18. You get the idea. So finally, maybe we want the last item from that new collection, and we'll get 30. So again, you get all of this stuff for free. So let's review one more practical example. Yet again, I'm going to grab all of our articles, but I want to group the items based upon their category or their tag, but I don't see any of that here. Okay. Well, we can leverage a technique known as eager loading.Okay. Well, we can leverage a technique known as eager loading. So I could say give me all the articles with their associated tags and get me the results. So now you can see for every article, we've also eager loaded the tags relationship, and the key thing to understand is this string here, with tags, that is corresponding to this method here. So when we say with tags, we're saying eager load the relationship with the name of tags. Okay. So great. So now at this point, there's a number of things we could do. Plucking, Flattening, Uniquing Tags 8:03So great. So now at this point, there's a number of things we could do. For example, what if I want to pluck all of the tags that are included as part of this collection? Maybe we're going to build up like a tag tree in the sidebar, but only the tags for the items we have currently fetched. All right. We'll take a look. Again, we have our articles, and it sounds like we want to pluck the name of each article, but it's not going to work quite like we think.Again, we have our articles, and it sounds like we want to pluck the name of each article, but it's not going to work quite like we think. So let me start with an example. If I were to pluck the title of each article, that would work. So now I have a new collection of only the titles for each article. However, if I were to say pluck the tags, okay, well, that's going to give me a collection of various collections, right? Because for each article, there's a number of tags. So each item has its own collection. So what we could do is pluck the tags and then flatten it all down.So each item has its own collection. So what we could do is pluck the tags and then flatten it all down. Or I could do flatten or even collapse, which is similar, but not for multidimensional arrays. So either one would work in this case. Okay. So now notice we have a new collection of only the tags. And then from that point, we could pluck the name of every tag. And now we're getting close. But notice we do have a few duplicates. And that's because some articles share the same tag, right?But notice we do have a few duplicates. And that's because some articles share the same tag, right? So at this point, it sounds like we only want the unique items in this collection. And there we go. Now if you're curious and you want to have a little fun, you could even tweak this a little bit. So for example, as you learn Laravel, you may learn about dot notation. So often you can, for example, load a view, articles.show. And that will just work. And the same is true for collections.And that will just work. And the same is true for collections. So I could say articles, pluck, and if I were to say, well, I have the tags, but specifically I want the name within them, it's not going to work. And that's because, again, you have a collection of tags. So what we can do instead is pseudo-flatten it by using this star here, tags.star, and then the name item for each of those within the subcollection. Okay. So now we get a new one. But now notice a few empty arrays.So now we get a new one. But now notice a few empty arrays. And that's because some of these articles don't have any tags associated with them. So yet again, we need to collapse or flatten them down. All right. And now we get the same thing again. And we could grab the unique items there. Okay. So yeah, in this case, the star basically serves the same purpose as a collapse. So we have our tags.So yeah, in this case, the star basically serves the same purpose as a collapse. So we have our tags. And then for each collection within it, we want to grab the name attribute out of it. And then from that result, collapse it down to a single level of values and give us the unique items within it. And we get Laravel, PHP, and education. So what we could do is save that to something like relevant tags. And you're all set to go. But now, one last thing for bonus credit. Maybe you want to echo these within a button or something like that, but you want themBut now, one last thing for bonus credit. Maybe you want to echo these within a button or something like that, but you want them to be uppercase. Okay. Well, of course, you could do UC words directly there. But if you want, yet again, you could map over each of the items and just return it there. All right. So now we have everything we need. I think that's pretty cool.So now we have everything we need. I think that's pretty cool.