Introducing Blade Loop Variable0:00
Okay, next up we have the loop variable in 5.3. Not a big thing at all, but still kind of good to know. So basically, when you use Blade's foreach directive, you now have a loop object available to each iteration, and that's going to give you details like, is this the first iteration through the loop? Is it the last one? What's the current index? What's the count? Stuff like that. That might be useful for setting classes and things of that nature.
Building Users Loop Example0:22
Stuff like that. That might be useful for setting classes and things of that nature. So let me give you a quick little example. Let's set up a handful of User objects. So maybe we'll say collect Joe, Jack, Susan, Jane. And then let's map over each one, and we will return a new User, all right, where we set the name. So we're just setting up four User objects. And now I'm going to load a Vue. Return Vue welcome, and we're going to send through our users.
And now I'm going to load a Vue. Return Vue welcome, and we're going to send through our users. Pretty basic stuff. Collect these, and then map over them so I can turn them into User objects. Just kind of a shorthand. So now if I switch over to the welcome Vue, as always, I could say for each user as a user, to start, why don't we just put them within a list item, maybe something like that. Okay, let's view this in the browser. And sure enough, we see each of the items in the list. Okay.
Inspecting Loop Properties1:19
And sure enough, we see each of the items in the list. Okay. However, let's do this. Let's var_dump this new loop variable so you can inspect it. Okay. So yeah, now you can see there's one loop for each iteration, right? So total of four. And like I said, it's just a simple data container that will contain its current index. So one, two, three, four. How many items are remaining in the loop?
Chunking vs Loop Logic2:33
And by the way, the exact same thing would be true for the last child. And now you'll see that switch over. Now let me show you one or two other use cases. You know what? You could do some of that stuff like where you say if loop is the first iteration or you know, where you use modulus like the current index mod four to determine if we have to add a div with a class of row. You could do that. But really for those things, I still think it's a lot easier to simply chunk it up. So because we have an Illuminate\Collection, I could just say chunk this into sets of four.
But really for those things, I still think it's a lot easier to simply chunk it up. So because we have an Illuminate\Collection, I could just say chunk this into sets of four. So as a userSet, and then we could say something like div row. And then I could say foreach $userSet as $user. You know, I still think that's kind of a cleaner way to go about this. But anyways, another thing that we could do is leverage the remaining property. So again, loop->remaining just returns how many items are remaining in the set. So what you could do is something like maybe something like this. Have you ever been in that situation where you're iterating through the collection and up until the last one, you want like a comma or a divider or a colon or something like
Comma Separation Techniques3:40
Have you ever been in that situation where you're iterating through the collection and up until the last one, you want like a comma or a divider or a colon or something like that? Very likely the answer is yes. So we'll do that. Really though in this case, if we just want a comma separated list of the names, don't forget like collections are your friend. So you could always say users implode, give me the name column and separate each one by a comma and a space. And really this is all you would have to do in real life.
a comma and a space. And really this is all you would have to do in real life. So if I refresh that, we get it. However, just for fun, let's say we're going to do it the long way. Yeah, we could do something like we don't need the UL by the way. But we could say we want the user's name. So this will just give us a space separated list. But then yeah, if we did that thing where you add the comma, well, it's also going to add a comma at the very end. So what you could do is something like if we have items remaining in the loop, then
add a comma at the very end. So what you could do is something like if we have items remaining in the loop, then give me a comma. Otherwise don't. And yeah, I think that would do the trick. And of course we'd need to get rid of that space. But yeah, that would be sort of the manual way to do it. And that's a good use case for this remaining property. So if you ever want to say, well, just as long as there are items after this current one, then add a comma or divider or whatever it happens to be.
Loop Variable Recap4:51
So if you ever want to say, well, just as long as there are items after this current one, then add a comma or divider or whatever it happens to be. Yeah, in those situations, this will work perfect. All right. And that's it. Nothing too complicated like I said. So in closing, whenever you use foreach, or this will also apply to forelse, by the way. Whenever you use those, you now have a loop variable that is a simple object that contains details about the loop. You'll find this in a lot of other template engines as well.
