Laravel Collections: filter, map, flatten - Practical Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today we continue reviewing native phpc website source code which is available on GitHub and today will be a final video of the series at least for now about collections I've been always fascinated by collection chains and while looking at this code I decided to Showcase it to you so there is a collection filter filter map flatten first and to understand what is happening here let's debug it together seeing all the values along the way and after looking at those maybe you will like collections as much as I do and will adopt it to your projects so to understand what is happening here the goal of that function is to redirect to some path only by URL so URL doesn't contain the full path to the documentation but laravel can take the first navigation item and automatically redirect to that so this is the result the path to redirect to the whole chain starts here with navigation what is the value of of the navigation let's see print R and die if we refresh this is the result but if we look at the source code of that then it's more pretty and we can take a look so it's an array of items each item has relative path and it has children so documentation sections like getting started and subsections like introduction installation configuration and others so this is our starting point and our goal is to get the first item from here but not necessar necessarily that let's take a look what does it filter first it looks for the page value the parameter in relative path values of those navigation items which in our case doesn't happen because page is null in our case so this doesn't happen and filter just returns true which means the same set of items nothing filter out here second filter is that we need to leave the navigation items only the items that have either path or or children again in our case these are all item this filter feels more like validation to filter out everything unnecessary or cleanup you would say and then from those filtered out navigation items we have a map map is basically iterating through items and then we return either navigation itself if we have a path or if we have children then we return children and here in my Tinker well I recreated that situation so there's navigation value like this and then after a few filters we have this we return children or not the result is the first filter unchanged the second filter unchanged as I mentioned but the third one will return nav children so each item of that array becomes with the new value of these ones and then finally from those values of children we flatten and take the first value let's see what does it mean so this is the code for flatten one and it returns array with five items so this one and then finally we take the first item from here we Echo that and we have three values Tinker well just Echoes them one by one but the actual result if we print it out as DD is this array of three items and then the final part of that function is check if the first navigation path is string then we'll redirect to that otherwise we'll redirect to the path of the array which is this one so with this example I wanted to show you how you can chain The Collection operations with Filter then filter some more then filter something else and map and flatten and do various sorts of operations and larel collections have a lot of methods if you want more practical examples of such collection chains I have a separate course on laral dailyc courses laral collection chains 15 real examples from open source those are all real projects with such combinations as map plus implode map and Max plug flatten reject map with keys and other collection methods so I will link that collection course in the description below what do you think about such combinations do you use them in your projects or do you prefer good old four Loops array methods and doing it step by step instead of one chain let's discuss in the comments below that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 4,736
Rating: undefined out of 5
Keywords:
Id: kSREDCyhZ1M
Channel Id: undefined
Length: 4min 31sec (271 seconds)
Published: Mon Apr 08 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.