Coding

What’s New in Laravel 5.3? – The $loop Variable

  • 00:00:00 oh sorry looping is fun especially if
  • 00:00:05 you know where you are
  • 00:00:06 that is why with lateral 5.3 we get this
  • 00:00:09 new loop variable available in our loops
  • 00:00:12 and with this variable we can always
  • 00:00:14 know where in this loop we are so let's
  • 00:00:17 have a look at it together
  • 00:00:20 and sneak peak at least at some of the
  • 00:00:24 ideas I have for some of that are more
  • 00:00:26 than ideas for the near future of this
  • 00:00:29 course but that's not what I actually
  • 00:00:31 wanted to talk about I want to talk
  • 00:00:33 about the new loop variable introduced
  • 00:00:36 in level 5.3 that's the variable I'm
  • 00:00:40 talking about
  • 00:00:41 and well what is it about s tonight
  • 00:00:44 implies you can use it inside loops here
  • 00:00:46 I do have a loop actually I do have two
  • 00:00:48 loops the outer loop and the inner loop
  • 00:00:51 here and I'm just feeding some data to
  • 00:00:53 this do you hear the data I feed is
  • 00:00:55 basically well an array which is kind of
  • 00:00:58 hard to read because I structured it
  • 00:00:59 really greatly here but that's just an
  • 00:01:01 array with some items and an array
  • 00:01:04 inside of the array so like this that's
  • 00:01:07 an extra rate inside of the array so I
  • 00:01:09 have a nested array and in my view I
  • 00:01:11 simply have outer loop and then I check
  • 00:01:14 if the item is an array so this nested
  • 00:01:16 array in which case I create another
  • 00:01:18 loop otherwise I output the item right
  • 00:01:21 away so far so simple now what's this
  • 00:01:23 loop variable doing well this variable
  • 00:01:27 gives us access to some nice utility
  • 00:01:30 there variables or features we can use
  • 00:01:33 inside of the loop for example here I'm
  • 00:01:35 finding out which the current iteration
  • 00:01:37 is so what's the number of the current
  • 00:01:39 iteration the iteration starts at 1 so
  • 00:01:42 not at 0 it's not the index
  • 00:01:44 it's the iteration and I check if the
  • 00:01:46 iteration is 2 then I want to gift this
  • 00:01:49 item a class of highlighted because 2
  • 00:01:51 happens to be the loop item here in the
  • 00:01:55 array which I want to highlight because
  • 00:01:56 that's this lecture
  • 00:01:57 now besides iteration which can be
  • 00:02:00 useful to find out where you're at in
  • 00:02:03 this loop we also get index which is
  • 00:02:06 almost the same but starts at 0 so here
  • 00:02:09 I would have to change this to 1 to
  • 00:02:10 achieve the same effect as you can see
  • 00:02:13 well there are a couple of other useful
  • 00:02:15 properties on this loop object for
  • 00:02:18 example we get remaining now I'll simply
  • 00:02:21 output this in the end of the death here
  • 00:02:25 and separate it with simply a dash and
  • 00:02:29 here our output loop remaining
  • 00:02:34 I'll simply copy this to my operative
  • 00:02:36 here too to see it in all cases now what
  • 00:02:38 would you guess to do or what would you
  • 00:02:41 guess that this does let's have a look
  • 00:02:44 it prints as the remaining iterations so
  • 00:02:48 here we have three remaining iterations
  • 00:02:50 at the beginning of the list then two
  • 00:02:52 then any nested loop we have six
  • 00:02:54 remaining five four three two one zero
  • 00:02:57 we're done and zero we're done with the
  • 00:02:59 outer loop so you already see the loop
  • 00:03:01 object always refers to the for each
  • 00:03:04 loop it's in so to the inner or the
  • 00:03:06 outer for each loop we got two distinct
  • 00:03:09 loop objects here which is of course
  • 00:03:11 great because this allows us to be much
  • 00:03:13 more flexible regarding information
  • 00:03:15 we're retrieving besides remaining and
  • 00:03:19 I'll just edit them both at a time I
  • 00:03:21 also get count now what could count be
  • 00:03:24 let's have a look counter simply the
  • 00:03:29 number of items or of iterations in the
  • 00:03:32 loop so we got four items here in the
  • 00:03:34 array well let's have a look if this is
  • 00:03:36 true we get this one to ten-year a free
  • 00:03:42 and then four and in the array we got
  • 00:03:44 seven which is why we here see seven so
  • 00:03:47 count is not changing it always refers
  • 00:03:49 to the total amount of items in the
  • 00:03:51 array but that can be useful if you want
  • 00:03:53 to have some if-else check in your loop
  • 00:03:57 checking if you're near the end for
  • 00:04:00 example if you're over half the items in
  • 00:04:03 the array or something like that that is
  • 00:04:05 where a count could be really helpful
  • 00:04:09 let's have a look at the next possible
  • 00:04:11 or the next property we can retrieve
  • 00:04:13 that would be first now what does first
  • 00:04:17 do let's let's see it in action okay
  • 00:04:21 it prints one and one so do you see what
  • 00:04:26 it does it actually returns us true or
  • 00:04:29 false if we are at the first iteration
  • 00:04:33 so we are at the first iteration of the
  • 00:04:35 outer loop here and on the first
  • 00:04:37 iteration of the inner loop here
  • 00:04:39 therefore we don't see any output in all
  • 00:04:41 the other cases now when could first be
  • 00:04:44 useful well basically first
  • 00:04:47 and last which you'll also see that we
  • 00:04:49 have are very useful properties as you
  • 00:04:52 oftentimes want to do something special
  • 00:04:54 right before starting or at the first
  • 00:04:57 iteration or right after finishing or
  • 00:04:59 you want to find out is this the last
  • 00:05:01 iteration
  • 00:05:02 really there is a useful feature and
  • 00:05:04 with first and well I already mentioned
  • 00:05:07 it of course we also get lost we get
  • 00:05:10 very useful utility features here so
  • 00:05:12 last will of course mark the last
  • 00:05:14 iterations and again keep in mind we do
  • 00:05:17 have two distinct loop objects for the
  • 00:05:20 inner and the outer loop now what if I
  • 00:05:24 wanted to access the outer loop from my
  • 00:05:27 inner loop so I want to have access to
  • 00:05:28 the outer loop object in my inner loop
  • 00:05:31 well I can use the loop Here I am loop
  • 00:05:36 parent property which gives me access to
  • 00:05:40 the outer loop object so on this parent
  • 00:05:43 I could then also access count for
  • 00:05:46 example so all the properties you just
  • 00:05:48 saw so if I do this well now we get four
  • 00:05:52 four four four even though the count of
  • 00:05:54 the inner loop would be seven as you saw
  • 00:05:56 before but now we're referring to the
  • 00:05:58 outer loop so we have this possibility
  • 00:06:00 of referring to the outer loop variable
  • 00:06:04 and we're not sticking or we don't have
  • 00:06:06 to stick to the one of our inner loop
  • 00:06:08 here the final property we have related
  • 00:06:12 to this is the death property so I'll
  • 00:06:16 add the death property to woops without
  • 00:06:19 parent though to both the inner and the
  • 00:06:21 outer loop or object and let's see what
  • 00:06:23 this does if i refresh we see one one
  • 00:06:27 and then two in the inner loop this
  • 00:06:30 basically gives us the death of the
  • 00:06:32 where this loop object is in for the
  • 00:06:35 outer loop it's the well out of most or
  • 00:06:38 or the most highest loop object we could
  • 00:06:41 have it's in the outer loop so we have
  • 00:06:42 one but then it's nested one level in
  • 00:06:45 the inner loop which is why we have two
  • 00:06:46 here if you would have an average loop
  • 00:06:49 what we would have free and so on so
  • 00:06:51 that can be useful to dynamically
  • 00:06:54 determine where you are at in your code
  • 00:06:57 or in which level of looping
  • 00:06:59 you are at so that's the loop object
  • 00:07:01 with all the properties just discussed
  • 00:07:04 definitely play around with it
  • 00:07:06 in the video description you not only
  • 00:07:08 find that code but also a link to the
  • 00:07:10 official description which lists all
  • 00:07:12 these properties see you in the other
  • 00:07:14 videos bye