My 5 Favorite Laravel Collection Methods

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this layer a bit i'm going to show you five of my favorite and most used methods when working with data in laravel collections if you're unfamiliar with what a collection is i'll show you real quick on the main documentation page for collections we can see right here it provides a fluent convenient wrapper for working with arrays of data what that means is is that we have this global collect method that we can pass in an array to that array can be flattened associative or nested and if we scroll down we have this huge list of methods that are available to us to use in those collections in order to manipulate the data that we provided all right so let's get started now i have here a laravel application that i stood up and if we go to the source code for it and if we go to routes web i've added in these five routes here all associated with a collection controller and if we go to that controller i've set up these five methods to use for testing purposes during this video each of these is going to showcase a particular collection method that either i like or i use pretty often so starting with the first one we'll create a simple collection using the collect helper method and pass in an array now let's provide it with a list of names now let's say we wanted to get back an array that only had names that began with the letter a and them so normally we would have to do something like a for each loop going over the array and determining if each one had an a in it but with collections we can use something called the filter method so collection filter and its only argument is a closure function containing each item of the array and then returning true keeps that item in the array while returning false removes that item from it and then filtered becomes a new collection containing only the items that we had filtered out so in this case if we want to make sure that the first letter is a we can return substr item 0 1 is equal to a and then in order to get the values returned back to us we simply do return filtered all all right let's head to our browser and see how this looks so we'll go to our first test route and you can see we've gotten an array back containing just two names andrew and andre and going back to the source code we can also use filter with nested arrays so we can say collection so we can say collect an array of arrays name andrew plants 50 name andre plants20 name jeffrey and plants 90. so then in the filter we can say return item where plants is greater than 30. and returning back to our browser and refreshing we have two items sent back to us both arrays where the plant's value is greater than 30. you might notice though that the keys here are unmodified using the original ones from the unfiltered array so 0 and 2. we can reset these by calling values before the all method so now refreshing we don't have the explicit keys anymore and our returns sanitize json all right let's move on so for this one let's create another new collection and like our previous one it'll be an array of arrays each one being a state followed by a capital i hope i spelled that right all right let's add in a couple more i don't know why i had to choose the ones with capitals i have the hardest i'm spelling and one more for good measure now this method is plucked so we can say collection pluck and denote a key like state in which case this will return back to us a flattened array containing just the states of that nested array in the collection so by returning plucked all and heading to our browser in our new collection 2 route up here you can see that we have an array back containing just the states in that nested array now let's say in this collection we also had another element called top cities that itself was an array containing a list of the top cities and we'll just get rid of this last element here so we have some room and copy that on to north carolina now we can call pluck on nested elements by using dot notation so we can do top cities dot one and this will return back a flattened array containing the value of the element with the one key in both of the top cities arrays we can see this by going back to our browser refreshing and we have a flattened array containing orlando and charlotte on to the third one let's create another new collection and like the first one let's start with a list of names this method is called contains and is called similarly to the first one so collection contains and it expects a closure function as the only argument with the item correlating to the current item in the array passed in unlike filter where return true or return false returns back that single item in the array the whole contains variable will be false unless one of the items in this array returns true so again using the example from earlier we can do return subscr item 0 1 is equal to a and so this will return true for two items the first two which means that this contains a variable here will be true so we can return contains and if we refresh our browser oh if we go to the actual new collection we see a one here which means that it returned true to make it a little easier we can say array does contain or array does not contain that's a little better now if we change this instead to be something like d there are no words in this array that start with the letter d and so refreshing will see that array does not contain so the contain variable has returned false also just like the top one we can use a nested array with it so we can copy this collection up here and let's paste it in place of this collection and we can say return item plants is greater than a hundred now there are none that have plants that are greater than 100 so refreshing we can see that array does not contain and if we change this to 50 and refresh again we see that it does and onto the fourth one this one is one of my personal favorites because of how interesting it is so again let's start out with a new collection and like the first few we'll use names now this method is called partition we call partition on the collection and much like filter and contains it uses a closure function containing the current item in the array like filter we run a conditional on it to determine whether or not the item meets a criteria so let's say count item is greater than five now what this does is going to return two collections back to us one where the items meet the conditional that's in there and one where they don't so it's kind of like using filter but also getting an array back with the items that were filtered out and we can get those items back with a destructured array so we can say above 5 and under 5. now if we return back the above five collection we should get back names that just have more than five characters in them oh actually this shouldn't be count this should be strlen all right let's check that out and i'm seeing an empty array i don't know why that's the case oh i didn't return the value of this i just called it we can see here we have an array containing three names that are all above five characters again the like filtered we have the keys in here that are present and we should call values first to get rid of those that's better and then instead we can call the below 5 array sorry under five and we have the two names that are five characters or less and just like filtered and contains we can also use nested arrays with these as well just calling the conditional based on the nested item that you want onto the last one now this one you might be familiar with because of its use in eloquent let's create a new collection and it'll be an array of arrays each one of these acting like a blog post so let's have a title and an author and we'll copy these a couple of times okay this method is called chunk and if you're familiar at all with the eloquent method what it does is pretty straightforward and it splits up a collection of items into smaller collections so we can say chunks equals collection chunk and it expects one argument an integer depending on the length of the collection that you want each chunk to be so if we say two here this will split it into two equal chunks each consisting of two items because we have four here and then if we return chunks all that should give us an array back consisting of two arrays of two items each let's check that out in the browser and there we have it we have two arrays each containing two items and because we're using collections we can chain on methods so we can return chunks first which should return just the first chunk and all and if we refresh in the browser we see that we're just getting back to the first chunk containing the two post items in it and that's about it i kind of just scratched the surface with this one but collections are a handy and pretty powerful tool in the laravel belt that can make working with arrays both big and small much easier
Info
Channel: Laracasts
Views: 4,230
Rating: undefined out of 5
Keywords: web development, php, programming, laravel, laracasts, jeffrey way, coding
Id: 8K1_ibgXd8E
Channel Id: undefined
Length: 14min 23sec (863 seconds)
Published: Sat Nov 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.