Building Collection Pipeline0:00
Now this next one is a small but very useful new addition. So here I have this concept of a Video, so we fetch a collection of all of them. And if I were to die and dump the videos, view it in the browser, sure enough, this is what we get, right? We have 10 of them. So now when you're constructing your collection pipelines, sometimes debugging can be a little tricky. For example, maybe you want to say, videos, and I want to shuffle them into random order, and then we'll say filter them. I'm going to use the higher order collection messages here. Filter them according to the videos that are archived. So I will call an archived method on each video. And then finally, let's pluck the title. All right, so let's update that, come back, refresh, and now we're going to have an array of seven videos, but specifically seven videos in random order that are archived. And real quick, if you're not familiar with this,
Higher-Order Filter Shorthand0:44
come back, refresh, and now we're going to have an array of seven videos, but specifically seven videos in random order that are archived. And real quick, if you're not familiar with this, it's just shorthand that I believe was added in Laravel 5.4. So when you see this, just think we're going to filter each video down according to whether it's archived. And this can be any method on the video. So in our case, if we switch over to video, we're calling this for each pass. So it's just shorthand, so you don't have to create the full closure there. And then finally, we pluck the title. So this is actually fairly simple, but as you can imagine for real projects, you're going to have some pipelines that are pretty complex, and debugging in those cases can get a little tricky. So often what happens if it's not working the way you expect is something like this, where you think, okay, well, is shuffling working right? Let's do that, run it,
Debugging with dump()1:28
can get a little tricky. So often what happens if it's not working the way you expect is something like this, where you think, okay, well, is shuffling working right? Let's do that, run it, and yep, that's working. We can see the random order there. So let's bring this back, and we're going to add this line. You know, I guarantee you've done something like this at some point in the past. But now rather than doing it like that, collections have a dump method as well as a dd method. So for example, we could say, well, just dump at this point in the pipeline, and whoops, let's remove that semicolon. But yeah, now it'll continue through that entire pipeline, but it will dump the collection exactly where you specified it. So now we can see right after we shuffle and we dump, yes, they are in random order. So then you could say, well, is it getting the archived items right? Let's just dump along there as well. And yes, before we had 10 items,
Using dd() vs dump()2:13
we shuffle and we dump, yes, they are in random order. So then you could say, well, is it getting the archived items right? Let's just dump along there as well. And yes, before we had 10 items, but now we have 7 items. So it did filter out all of the videos that are not archived. And then finally, yeah, if you kept going, you get the basic idea at this point. Let's just dump out the title and then give that a shot. Yeah, now we can see we get the array, but before that we had the archived items, and before that the full random list. Really useful. Now, like I said, the only difference is if you were to set dd, well, of course, that will dump and then kill the execution. So in this case, we'll just get a collection of the 10 items in random order. And that's all there is to it. So really useful stuff. And of course, just make sure when you're done debugging, you get rid of those entirely.
