Laravel Collections & Eloquent Methods: 44 Essential Methods for Beginners - Mastering in Laravel

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up developers Zari here and welcome back to a new midi course where I'm going to have a deep dive into collection methods in laravel in this video we will talk about the power of collections we will walk through setting up a new larval project we will set up a database connection and we will create a migration model and Factory to play around with our collections now what are collections collections are an extremely extremely powerful and versatile data structure that are widely used in programming at their core collections are simply an array but they offer so much more in terms of features and flexibility that they have become a Cornerstone of modern programming over the last couple years a lot of people ask me where they should use collections over arrays and I always responded saying that collections are basically arrays but there are on a lot of steroids one of the key advantages of collections is that they allow you to group related objects together in a logical and easy to use way whether you're working with simple strings or complex custom objects collections can help you to organize and manipulate your data with ease welcome to my udemy course where we'll explore the ins and outs of larva's query Builder eloquent and RM and relationships we will start off by diving into migrations Cedars and factories to get a good understanding of databases then we will dive into the query Builder where we will learn how to craft complex SQL queries using larval's elegant syntax from there on we will move on to eloquence Largo's powerful orm which allows us to interact with our database using intuitive object-oriented syntax since we're on the topic of eloquent we will also explore relationships in laravel which allows us to Define how our database tables are related to another making it easy to retrieve and manipulate data across multiple tables from simple relationships such as the one-to-one relationship all the way to polymorphic Relationships by the end of this course you will have a deep understanding of migrations factories Cedars query Builder eloquent orm and relationships where you will be able to build a complex web application with ease I'm looking forward to see you in this course now let's get started I've already talked enough let's set up a new larval project which we need to do through the command line interface where we need to perform the composer create Dash project dash dash prefer dist larval forward slash larville and let's name it laravel Dash collections I'm going to cut out the part where I'm going to open my project and phpstorm and I'm also going to use Tinker well in this course so pause the video set everything up and I'll see you back once you're done now tinkerwell is a PHP Ripple tool that allows developers to interact test and debug their codes it is useful for testing purposes because it allows developers to quickly test code Snippets and see the results in real time without having to actually write a full script or run the entire application quick note this video is not sponsored by tanker well I recently started using their product and when building my database in eloquent course I found out how Tinkerbell makes a life of a developer easier and especially for a tutor explaining the codes so the first thing that we need to do is to navigate to phpstorm where we need to open our DOT EMV file because we need to change up our database credentials I'm going to set my passwords first to Dari one two three four my username is roots my database name will be larvo underscore collections the porters are right the hostess are right in the connection as well we obviously need to create the database so let's navigate to the City Line and let's perform the MySQL where I'm going to perform the create database and let's name it laravel underscore collections that's before been exit right here to exit my sequel since we're going to interact with the database I quickly want to create a new model and migration to have some test data inside my database we could use a user model in migration but the downside of using it is that it does not have that many different data types and if we create a new migration ourselves we could use all the different data types that we could perform operations on so let's change directories into our project by saying CD laravel Dash collections and right here we're going to perform the PHP artisan make me a new model let's name it article we're going to add a dash m flag to create the migration but we're also going to add the dash F5 to generate a factory once we hit enter you'll see that 3D files have been created so we're going to start off with the migration so let's navigate back to phpstorm we don't need our DOT EMV file anymore so let's close it off open the database directory migrations and we need to open the latest migration that we have created now if I scroll down right here you'll find the schema where we can create our articles table now I want to keep my primary key as the First Column so in between the primary key and the timestamps we're going to create a foreign Key by saying table object foreign ID before an ID column name will be user underscore ID we're going to chain the constraint method to it because it constrains the table users and we're going to add the Cascade on delete method then I need another column so let's use our table object again the data type will be a string so basically a far Char the column name will be title and the title needs to be unique then we have our description so let's say table which will be a long text of description let's add another column which will be a text data type and the column name will be an excerpt and the excerpt can be nullable now I want two more columns one Boolean and an integer so let's add a table Boolean which has a column name of is underscore published and it has a default value of false then I have a table integer which has a column name of minutes to read which can also be nullable before we navigate to our article model let's quickly open our users table because I want to add an additional column which will Define the user rule this will not be the best practice when it comes to adding rules to Applications but it is just the simplest way of adding it and playing around with their collections so right under our email verified ads we're gonna add a table string the column name will be row and we're going to chain a default of let's say user now let's navigate up let's open our app directory models and let's open the article model because one thing that I always forget is defining the protected fillable property which we need to set equal to an array and inside our fillable property we need to add attribute names that are mass assignable this means that you can create or update models using the create or update methods only the attributes are listed in the fillable property will be allowed for Mass assignments all other attributes will be guarded and cannot be Mass assigned and this is basically a security feature that helps protect against Mass assignment vulnerabilities so let's add our user underscore ID let's say the title let's add the slog we're going to add the description we're going to add the excerpt we're going to add the is underscore published and we're going to add the minutes to read so let's also open the user model and let's scroll down and inside the fillable property let's add the rule as well the last thing that I would like to set up is my article Factory in laravel we use factories to quickly generate dummy data for testing and seeding our databases it basically allows us to define a set of attributes for a model and generate multiple instances of that model with randomized data which is pretty helpful for testing and development purposes as it allows us to create a large amount of data and test various scenarios without having to manually create each record and since we're not going to work with an actual working application but simply going to play around with queries we could use dummy data so let's navigate to our Factory directory open it and open the article Factory where we have our definition method where we need to define the default attributes for a model so usually what I do is starting off by defining the keys first so let's say user underscore ID is equal to an empty string we have the title we have the excerpt we have the description we have the is underscore published and we have the minutes to read now let's define the values right now for our user ID we're going to use our user model where we're going to access the all method but we're going to chain the random method to it which will grab a random user where we only need to have the ID then we have the title which comes from the fake method the title needs to be unique and it needs to be a sentence then we have the excerpt which also comes from the fake methods we're gonna chain the paragraphs method to it where we need to pass in two arguments the length which needs to be two sentences and we have true which says that it needs to be as text now the same line needs to be defined for the description so let's do that but the description will be eight lines and it's true as well then we have our is underscore publish column which is a Boolean so let's use the fake method and chain the Boolean method to it and finally we have the fake method which will be a random number which will be between 1 and 10. all right now let's navigate to our user Factory for a moment because we do need to add an additional key value pair right here as well so let's go right below our email verified at now let's say that the rule of a user is coming from the fake method it's a random element where we're gonna pass in an array with three items we have a user we have an admin and we have a super underscore admin we're ready to perform a couple commands in the CLI so let's navigate back to iterm and let's first run the PHP Artisan migrate command to run all migrations that we have inside our project all right that was successful secondly we need to run PHP Artisan Tinker because we need to generate domain data for both the user model and the article model so inside our article Factory we said that the user ID column should have a value from the user model so we first need to create users before we could create articles so let's say user column column Factory chain create in the factory method we need to pass in an integer of the amount of users that we want to create so let's say 50 . as you can see it has created 50 new rows because the last ID is 50. then we need to perform the same command to create article so let's say article model Factory create now let's pass in 100 articles in case you're being prompted with a model not found exception right here your model namespace is most likely not being correctly Auto loaded you could either Run exit and run the composer dump Dash Auto load command to reload the autoloader or you could simply hit the arrow off and add in model namespace in front of the article model and usually I just find this a little bit easier so let's say app backslash models backslash once we perform our request you'll see that we have created 100 new rows from now on I want to use Tinker world to play around with collections using tinkerone allows me to show you what the query speed is and it also shows what happens behind the scenes you don't need to have Tinker world to play with collections you could continue on in the same exact way as we have been doing up until this point Tinkerbell also does not have his own syntax I have to make that clear it literally uses laravel so whatever I'm doing you can do it too so I'm going to quickly set up the routing for developers that are going to follow this course but don't have Tinker well so let's perform an exit right here and let's perform a clear as well and right here we need to create a new controller through the PHP artisan make me a new controller and we're going to name it article controller then we need to define a route so let's navigate back to PHP store let's open the rights directory web.php file where I write under our welcome route we're going to use the route method and it's going to be a get request the endpoint will be forward slash articles the controller will be article controller class comma second value of index which will be the index method Insider articles controller which you basically don't have so let's navigate up open the HTTP directory controllers and article controller where we're going to define a public function index so whatever I'm going to do inside Tinker well you could simply replicate by defining a variable setting it equal to inquiry and then add a dump die to it we're going to cover a methods to calculate the average of a collection the count the minimum the maximum number the median the sum and we're going to retrieve a random number from a list before we continue on with the scores I want to show you two different ways on how you could use most of the methods we will be using in the scores in laravel we could use a static method that can be called on a Model class which will look like this we're going to call our article model where we can add a colon colon where no method and change the count method to it but the same collection method of where no can also be called on an instance of the query Builder to add a where Clause to filter the records based on the value of a specific column that is null both methods can be used to achieve the same results but they are called in different ways and this is how it looks let's say that we have an article variable which has a value of a collection where we can chain the where no methods to it but throughout this course we will be playing around with both methods but I just wanted to make sure that we're both on the same path the first method that I want to cover is the counts method which Returns the total number of records for a given model now let's say that we want to see how many articles we have to find inside our application where we could simply make use of our article model where we're going to use the count methods where you will see that it has returned a total of 100 records this is quite simple but keep in mind that we could also use the count method in combination with the where method or any other methods the where method is used to retrieve records that match a specific condition and since our article model has a is underscore published attribute we could basically say well get me the article model chain the wear method to it now let's say that we want to get the values for the is underscore publish column is equal to true this doesn't count anything because right after the where method we could chain the count methods now let me make this sidebar a little bit smaller alright so what we have done right here is called meta chaining a method chaining a larva is a technique that allows you to call multiple methods on an object in a single statement this is often used in alcode queries where you could chain methods to build complex queries once we perform a request you will see that it has returned 44 articles where there is underscore publish column has been set equal to true now let's move on to the second method which will be the count by method the count by method is used to group the records by a given column and Returns the count for each group so let's say that you want to know how many articles you have for each author quick note the count by Method only works with columns that contain integer values because it is not possible to group records by non-numeric values if you try to use count by with a non-integer column you will receive a type error exception so we need to make sure that we first plug a column so let's say article and we're going to pluck the user ID column right then right after the plug method we can chain the count by methods once we perform our request you will see that it returns an associative array where the keys are the author IDs and the values are the corresponding article counts the next method is the max method and the max method Returns the maximum value of a given column we obviously know the maximum value of the user ID column since we have that on our screen right now which is 50. so let's say that we want to retrieve the maximum value from our article model which has the longest read of posts so let's say article colon colon Max and it needs to Max the minutes to read column which will return the highest Minister read among all articles in your database which is equal to 9. so let's see how the max method can be useful in a bit more complex query in combination with aware between method so let's say article or where between the user ID needs to be equal to comma an array between a value of 20 and 30. where we could then chain the max method to it where we need to pass in a column name of minutes to read this query will return the highest Minister reads amongst the values of our articles where the user ID is between 20 and 30. next to the maximum number we also have the minimum number which you can grab through the Min methods the main method simply Returns the minimum value of a given column so what we could do right here is basically say well article colon colon Min and a minimum value is the minutes to read column well basically off the ministry column excuse me the next method is the median method which is used to retrieve the middle value of a set of numbers when a set of numbers is ordered from smallest to largest or the other way around the median is the value that is exactly in the middle you might wonder when calculating the median is useful and think about a scenario where you need to analyze salaries test scores or other numerical data sets so let's say that we're going to use our article model again we're gonna pluck a specific column which will be the minutes to read and we're going to then chain the median method to it and this will return the median number of the ministry column among all the articles in your database the next method is the mode method retrieving the mode can be very useful when you want to find out the most frequent occurring value in a data set so let's say that Insider article model we're going to once again pluck the user ID because I want to see which user created the most articles so let's change the mode methods to it which will return an array with user IDs so it could also be multiple now we could even make it a little bit more complex by finding the most common user ID among all articles that are published so let's say article where the is underscore publish column is equal to true and let's change the plug method to it because we're going to plug the user ID and then we're gonna chain the mode methods and let me actually align the chain methods on the line below once we perform our request you'll see that it's still the user where the user ID is equal to 48. we could also retrieve a random record from a given model using the in random order methods and we have already done this when defining our article Factory but let's do it again so let's say that we have our article model let's use the in random order method if we perform the request right now you'll see that we don't retrieve anything because we do need to change the first methods to get the first value once we perform it again you will see that we have retrieved a article where the ID is equal to 52 and if we keep performing our request you will see that it keeps changing up the road that we're retrieving you could also retrieve a specific value where you need to replace the first method with the value methods but with the Value method you need to pass in the column name that you want to retrieve so let's say that we only want to retrieve the ID which will retrieve the ID value finally we have the submit it which Returns the total number of a given column so let's say that we want to retrieve the total minutes to read of all articles so let's say article colon colon sum and we're going to sum the minutes to read column once we perform our request it will return back 511 meaning that if a user wants to read all articles inside their database it will take him 511 minutes one of the most common methods when it comes to collections is the where method and the where method filters The Collection based on a given key value pair if you have used MySQL before which I actually expect you to have you might think that the wear method is similar to the where clause in my sequel it partly is but it's not exactly the same the wear method in collections is used to filter a collection based on a specific key value pair or a closure where the where clause in my sequel is used to filter data from a database table based in a specific condition the wear method is executed on a collection object in memory while the where Clause is executed by the MySQL server so there are some differences between both so I have created a simple collection which you can see on my screen right now where I have store two keys of name and age with their respective values now let's say that we want to retrieve an item where the age is equal to 22. so let's use your collection variable where we could chain the wear method to it the where method takes two arguments the first argument is the key of the item in the collection you want to filter by which in our case will be age and the second argument is the value that you want to filter by which in our case will be 22. now quick note the default operator of the where method in larval is equal to whenever you want to use a different data type you need to pass in a second argument of the operator that you want to use so we can set it equal to greater than not equal to less than and so on but since we want to check for a specific match we can simply remove our second argument once we perform our request you will see that the where method has returned a new collection that contains the items that have passed the given truth test which is a user with the name of mic where the age is equal to 22. so let's add an argument in between both where we need to have the H that's higher than 22. once we perform our request you'll see that it has returns three users now next to using it on the collection we have to find ourselves we could also use the where method on our article model let's give it a try let's say article colon colon where right here we're going to check whether the is underscore publish column is equal to true once we perform our request you will see that it returns nothing and this is happening because we do need to change the get method at the end of the query to retrieve the results of a query without that the query will not actually be executed and no results will be found once we perform our request right now you will see that it has retrieved all articles that are published now a pretty interesting feature that Largo has to offer is that you could chain multiple rare methods where multiple filters will be added based on key value pairs so let's met the chain on the line below or we're going to add a second where method where the minutes to read needs to be equal to 9. once we perform our request you'll see that it has retrieved articles but way less than before because we have added a second wear condition well let me actually add enters right here all right finally instead of passing and key value pairs inside aware method you could also pass in a callback function in the Callback function we can write any logic that we want now we're going to pass in a variable of query inside our callback function which will be used to filter the article model in our Copic function we can return the query where we could chain the wear methods and you might wonder right here why do we need to use the where method again well the Callback function has access to the entire item that's being filtered whereas the wear method is limited to filtering based on key value pairs this allows for more advanced filtering criteria to be applied to the collection or query so what we could do right here is basically say well get me the post where the is underscore publish column is equal to true we finally need to change the get method to it let me actually make it a little bit smaller again all right once we perform our request you'll see that we have retrieved all articles that are published the next method that I want to cover is the where strict method which uses a strict comparison operator a strict comparison is a comparison between two values that checks for both the value and the type of the values being compared so let's see how it works with the collection we have used in the previous example let's change the first name inside their collection to lowercase diary then we could use our collection variable and chain the where strict method to it we need to add two arguments first we need to pass in the key which will be named and secondly we need to pass in the value we want to search for which will be Capital Dari in the output you'll see that it has retrieved one user where the name is equal to Dario with a capital D now in PHP the triple equal operator is used for strict comparisons while the double operator is used for a Loosely comparisons when using the rare strict method in larval collections a strict comparison operator is used to filter The Collection this means that only items with an exact match of the given key value pair will be included in the filtered records finally we have the WHERE between methods which is used to filter a collection based on the range of values and we're going to use it on our article model this time so let's say where between it takes two arguments the first argument is the key of the item of the collection that you want to filter by so let's say the minutes to read column where the second argument is an array that contains two values the minimum value and the maximum value so let's say 5 and 9. now finally let's not forget to chain the get method to it all right now this Command right here will retrieve all articles where the ministry column has a value between 5 and 9. now quick note you could also use the where between and where method together so let's say that we want to chain the where method to it as well but we're going to change the search on the where between method to the created underscore add column because we're going to retrieve all articles that are published between January 1st 2023 and June 30 2023 and that have at least 5 minutes to read so we need to replace the integers inside our array let's hit enter right here now let's say that the date needs to be 2023 0101 where it has a second argument of 2023 06 so June and 30. and then we could Define the where methods so let's say where the minutes to read is greater than let's say five so once we perform our request you'll see that it has retrieved all articles that have been published between January 1st 2023 and June 30 2023 and that have at least a minimum of five minutes to read now the advantage of using the where between method is that it allows you to easily filter a collection based on a range of values it can be useful in situations where you need to retrieve items that fall within a certain range so let's redefine the query that we have without the wear between methods so it will look like this so let's say article where get so the first where Clause will be the created underscore at needs to be greater than or equal to 2023s January the 1st then we can chain another wear method to it where the created underscore add column needs to be less than or equal to 2023 0 6 30 then we need to add another where method where the minutes to read is greater than 5. and in situations like this I simply prefer to use the where between method because it will eliminate one where method so let's start off with aware in method which allows you to filter a collection based on multiple values it is often used when you have an array of values and you want to find all the records in a collection that match one or more of those values to demonstrate how the where in method Works let's take a look at the scenario let's use our user model this time now let's find out all the users that have a certain rule let's first Define an array named rules and let's pass in two values right here let's say admin and super underscore admin then right outside of our array we can use our user model and then use the where in method and change the get method the wherein method accepts two required arguments the first argument is a column that you want to filter on which in our case will be rule while the second argument is an array of values you want to filter by so let's pass in our rules array quick summary of our example right here we are using the wherein methods to find all the users that have a rule of admin or super underscore admin once we perform our request you will see that it have retrieved a lot of users where their role is either an admin or super admin this output isn't very clear so let's replace the gut method with the count method that we have used in the second episode and let's perform it where you will see that out of the 50 users that we have created 36 of them have a rule of either admin or super admin now many developers would use the where method in this case but using the wherein method is so much more powerful because it allows you to find records that match each value in an array you can use where in to find all the records in one query I also think that the wear in method makes your code more readable and maintainable by using the expressive syntax of larva's collection to write codes it is easy to understand and to modify next to the wherein methods larva offers the where not in method which is useful when you want to exclude specific values from your query using the aware not in method has the same advantages as the where in method because it makes your code more readable and maintainable it is often used when you have an array of values and you want to find all the records in the collection that do not match any of those values let's use our user model again and let's use the where not in method now let's change the get method to it just like the aware invented it accepts two arguments the first argument is the column you want to filter on which in our case will be raw again while the second argument is an array of values that you want to exclude so let's say that instead of including the admin and super underscore admin rule we're going to exclude them now once again if we perform a request we will retrieve quite some rows so if we replace the get method with the count method you'll see that we have retrieves 14 rows which is correct because the where in method retrieve 36 rows and if we add both of them it's a total of 50 users now just like any other method in laravel you can chain as many methods as you want so let's say that we want to change the where method right here and that we only want to retrieve users where the email verified at is equal to today which is 2023.04 13. in the output you will see that it has returned zero and that's because the email verified add column of the users have all not been set but the point is that you could use the awarenet in anti-wear in method with any other method that you want sometimes when working with data you may encounter situations where you need to filter out records where a specific column has a null value this is where the where no method comes into play in larval the where no method is used to filter The Collection based on the values of a specific column that is null it is a method that is commonly used in conjunction with the eloquent module and one of the reasons why the aware no method is so powerful is that it allows you to filter through large collections of data pretty quickly and easily and besides it's also a very great way to retrieve all the records from a database table that have a no value in a specific column when defining our migration we set that the excerpt column could be nullable but Insider article Factory we also said that the fake method should add a value to it so let's update a couple rows Insider database before we continue on so let's define our article model and let's use the where method first where we're gonna find articles ready minutes to read are greater than let's say eight then we're gonna chain the update method to it because we're going to update all those rows and we're going to set the excerpt equal to null once we perform our request you'll see that it has updated 14 articles so let's get rid of a query because we don't need it anymore let's see how the where no method works so let's define our article model again and let's use the where no method and we need to change the get method to it the awarenual method requires one argument which is the name of the column you want to filter we just updated a couple records inside our database where we set the excerpt equal to null so let's pass in a string of excerpt right here now this code will return all the Articles from the Articles table that have a null value in the excerpt column to double check that let's replace the get method with the count method where you will see that it has retrieved records which the update method also returned back so this is correct now let's quickly see how this works on an instance method that we can call so let's define a new variable named collection and let's set it equal to the collect method pass in an array another array where we have a key of name and a value of desk and right outside of it we're going to add a comma and add another array where the key is name but the value is equal to no then right under our article where no query we could basically use our collection variable and chain the where no methods to it where we need to pass in a key of let's say name once we perform our request where you will see that it has returned a new collection where the name is equal to null now next to the aware known method larva offers the complete opposite which is the where not no method and let me scroll down a little bit all right an awarenule method is used to filter The Collection based on the values of a specific column that is not nil it is useful when you need to retrieve records from a database table that have a non-known value in a specified column we could technically use the same exact use case that we have on our screen so let's say that we want to filter The Collection that we just defined and only show the items where the name has been set to achieve that we can use our collection variable change the aware not no matter to it pass in a key of name now this code right here has returned all the items from the collections variable that have a non-null value in the name which is in our case desk now the aware not normative can also be used as a static method to be called on a Model class so let's say article colon colon where not no and let's get the values now let's say that we only want to retrieve rows where the excerpt is not null once we perform our request you'll see a complete list so let's replace get with count where you'll see a total of 86 rows now let's say that you only want to retrieve articles that were published in 2021 or all articles published on a specific day of the week and these type of scenarios you could use these methods let's first retrieve all articles that we have and let's copy one value of the created underscore add column let's see where it is right here I'll just paste it as a comment right below our query now let's start with the where date methods the where date method is useful when you want to filter your database query results based on a specific date let's replace the all methods with the where date method unless chain the get method to it as well as accepts two required arguments where the first one is a name of the column that contains the date you want to filter it by which is created underscore at while the second argument is the actual date you want to filter on so let's say 2023 0 4 13. behind the scenes this query will generate a SQL query that filters the results based on the date portion of a daytime column so if the date portion matches the row will be included in the result set let's see how the query looks behind the scenes well let's perform the request and this might be too small for you so let me copy it and add it as a comment right here where you will see that it performs a simple select statement where it will select all fields from the article table what will extract the date part from the time using the date function where it passes in the created underscore add column where the date needs to be equal to the 13th of April quick note you could also add other operators as a second argument so we could basically say well one moment it needs to be less than greater than less than or equal to or greater than or equal to but in our case we're just gonna get rid of it now let's delete the comment as well because the second method that I want to cover is the where they methods this method is perfect for when you want to filter your database query results based on a specific day of the month it is powerful because it allows you to retrieve all articles published on a certain day regardless of the year and months now let me repeat myself regardless of the year and months so let's perform our query let's say article colon colon where they now let's change the get method the where day method takes two arguments the first argument is the name of the column that contains the date you want to filter by which in our case will be created underscore at again while the second argument is the specified day of the month you want to filter on not the day in a full string but the integer number of the day of the month so let's say 13. now once we perform a request you'll see that it obviously returns a full list of Articles so let's replace the get method with the count method where you will see that it has retrieved 100 rows and that's correct because we created 100 rows using our Factory class all on the same date now behind the scenes this command will generate a SQL query that filters the results based on the day portion of the daytime column once we perform our request you'll see that the biggest difference between both queries is that the second query uses the day function rather than the date function the next method on the list is the where month method it is powerful because it allows you to retrieve all articles published in a month regardless of the year and let me repeat myself one more time is regardless of the year so let's perform a query let's say well let me add a couple enters right here let's say article where months get it accepts the same arguments so the name of the column that contains the date time value you want to filter by which is again created underscore at and it accepts an integer value of the amount you want to filter our schools on so let's say 11 once we perform our request right now you will see that we receive an empty collection which is correct because all of our articles have been created in the month of April so if we change our second argument to 4 and perform our request you'll see that we have retrieved all the Articles again now you probably wonder whenever you should use the where months method this can be useful for a block where you have a lot of deals in the month of November so think about the Black Friday deals cyber monday deals and so on and you want to retrieve all deals of the last years the next method is the where year methods which is also very powerful because it allows you to retrieve all articles published in a certain year regardless of the month and day so let's say that you have a block and you want to create a feature that shows all blog posts published in a certain year this is where it can be very useful so once again let's define our article model and use the where year method just like the other methods that we covered it accepts two arguments the first argument is the column that contains the daytime value that you want to filter by which is created underscore at and an integer value that you want to filter articles on so let's say 2023 and let's actually replace the get method with account methods once we perform our request you'll see that we retrieved 100 rows finally we have the WHERE time method and the wear time as it is useful when you want to filter your database query results based on a specific time so let's say that you want to retrieve all articles that were published between 9am and 5 PM so let's make use of our article model use the where time methods now let's say count it accepts once again two arguments where the first one is the column so created underscore at while the second argument is the specific time you want to filter on which should be a format of H of hours colon mm for the month colon double s for the seconds in a string so let's say 9 am and what we need to do right here is actually add a operator in between so let's say it needs to be greater than or equal to and then we're going to chain one more method to it which is the where time method again where you created underscore at needs to be less than or equal to let's say 17 hundredths and I forgot a comma right here once we perform our request you'll see that we have retrieved zero articles for the filter method in laravel which is a very useful method when you want to manipulate collections of data in a more efficient and elegant way let's first Define a new variable named collection and let's set it equal to the collect methods we're going to pass in an array right here with four integer values so let's say one two three and four let's use our collection variable and let's change the filter methods to it once we perform our request you'll see that it pretty much printed out a collection of our four items nothing special because it has nothing to filter out a pretty interesting fact about the filter method is that it removes empty values by default and that's because they are considered falsely in PHP so what empty methods do we have well let's define them into their collection we have a empty string we have no we have false we have zero and we have an empty array once we perform our request again you will see that all elements that are empty are evaluated to false and have been excluded from the filter method you could also once again see that besides removing empty elements nothing special has apped and that's because the filter method does not actually perform any filtering until a closure function is passed in as an argument to do that we need to basically say well function parentheses curly braces and hit enter so we also need to pass in an argument in the closure right here which will represent each element of the collection so let's pass in value now this is obviously required because the function needs to indicate whether or not each element of the collection should be included in the filtered list the closure function returns true or false depending on whether the element should be included in the filters collection so inside the filter method we need to return something and that's something will be a value that is greater than 2. once we perform our request you will see that it has returned a new collection with key value pairs where the keys are the collection keys and the values are the collection values but one thing that I'm noticing right here is that it has added the empty array and this is happening because it's not an empty value like an empty sting null false or zero therefore it evaluates to True when compared to 2 and is not filtered out now like I've just said it adds Keys as well right here which are equal to the keys that you have in your default collection but this also means that we could even access the keys and this can be done by passing in a second argument inside the closure function of key then inside a return statement we can add a double Ampersand and say well the he needs to be less than 7 as well once we perform our request you will see that it has removed our array because the key of our array was equal to 8. so you might wonder what the biggest difference is between the filter and the wear method because the functionality we added right here could also be performed with the where methods the filter method returns a new collection with only the elements that pass a given true test while the aware method filters The Collection by a given key value pair in other words the filter method is more flexible and can be used to filter a collection based on any condition while the wear method is more specific and is used to filter by a specific key value pair now let's remove what we have done right here and let's have a look at a more advanced example let's say that we have a new variable named articles and we're going to use our article model the wear methods and we're going to get the count and what we're going to do inside our wear method is basically retrieving all articles that are published so let's say is underscore published is equal to true now once we perform our request it simply returns 44 rows so this works fine so let's replace the count method with the get method and since eloquent returns a collection I want to use my articles variable and I want to change the filter methods to it so let's pass in a closer function so function parentheses curly braces we're going to add an argument of article then inside our filter function we're going to return the article or the minutes to read is greater than 8. once we perform our request you'll see that we have retrieved a collection where the minister read is greater than eight so like I've said before the filter method is very useful when you need to extract a subset of elements from a collection that meets a specific criteria one important thing to note is that the reject method is the inverse of the filter method while filter returns a new collection with only the elements that pass the given truth test reject is the opposite and returns a new collection with only the element that fails the given truth test this makes it a powerful tool for filtering out unwanted data from your collection I'm going to perform the reject method with the same collection that we have inside the previous episodes where we have used a simple variable named collection which has been set equal to a couple integers and falsely values now let's use our collection variable now let's change the reject methods to it once we perform our request you will see that all falsely values have been inserted into a new collection and all the integer values for my collection have been removed the reject method also accepts one argument which is the closure function so let's say function parentheses curler braces which will determine the truth test for each element inside the collection now the closure function takes two arguments the value of the element which we will set equal to variable value while the second argument is its key so let's set it equal to variable key then inside our closure function we can use the value and key arguments to determine the truth test for each element in the collection so let's say that we're going to return the empty method which is a built-in PHP function that determines whether a variable is empty or not and we're going to pass in the value once we perform our request you will see that we have filtered out all the falsely values and it has added the values that are not empty and if we replace the reject method with the filter method you will see that it's the inverse so it has filtered out all the integer values and it has only added the falsely values now let's use the reject method with our article model let's first retrieve all articles so let's say article column column all quick note this isn't the best working example because it isn't best breakfast to retrieve all articles and then filter it but it is the best possible way to show you how this works with database data rather than just a simple collection with integer values so let's say that we want to use our articles variable and we want to chain the reject method to it we're going to pass in a closure function where we're going to add an article argument where we're going to reject all articles where the excerpt has not been set so let's say return empty article excerpt once we perform our request and we'll scroll to the top you will see that it has retrieved a new collection with only articles that have a non-empty excerpt it basically removes any element that does not pass to True fast which in our case is checking if the excerpts field of the article is empty or not now if you have been watching this course and you probably have obviously you might have noticed that we could also use something like article where not no get and the excerpt cannot be no which is correct but there is a slight difference right here both methods will return a collection of articles that have none empty excerpts but the reject method can be more flexible since it allows you to pass in a custom callback function to determine the truth test for each element in the collection obviously the check that we're performing right now can be used for the where not know method but let's say that instead of the empty method we want to make sure that the article excerpt is numeric for example which will be a little bit more difficult with the aware not no method or any other method because there isn't a method for it and this is where the real power of the reject method comes in clutch we're going to start off with the contains method which does exactly what the name of the method says it literally checks whether a value exists inside the collection so we have our collection defined which we're going to use because we're going to chain the contains method to it the contains method takes one argument which is the value you want to check for in the collection so let's pass in three once we perform our request you will see that it returns back with true meaning that the value does exist if it doesn't exist it will return false in our case an integer of 3 does exist so let's change the value 2 to 30. once we perform our request you will see that it has returned backward false meaning that the value does not exist inside the Library as simple as that the second method that I want to cover is the accept method which is used to remove one of the items from an collection now before we could use it properly let's actually Define a collection with key value pairs so let's set our collection variable equal to a decorate method let's pass in an array with three key value pairs the first one is name which will be equal to Kevin De Briner then we have the H which will be set equal to 31 and we have the club which is equal to Manchester City so whenever you want to use the accept method you first need to use your collection variable and chain the accept methods to it the accept method takes one or more arguments where one is requires and all the other ones are optional which can be as many as you want that will represent the keys of the items you want to remove from the collection so let's say that we love the name of Kevin o'briener we also love the club but let's get rid of the age now what this will do is simply filtering through the collection it will try to find a match where the key is H and it will create a new collection with a specified items removed the original collection is not modified well to actually create a new collection we need to set the accept method equal to a variable so let's say filtered right now we will create a new collection named filters which is a key of name and a value of Kevin De bruyne and another one of club and Manchester City now like I've said we could also pass in multiple keys so let's pass in a comma let's say Club where it will only show the name of the player next to only picking key value pairs you could use the only method which will retrieve one or more items from a collection so what I want to do right here is to find a new variable named filter it and I want to set it equal to our collection variable and chain the only method to it it works pretty much in the same exact way where it takes one argument that is required and all the others are optional which can also be as many as you want that will represent the key of the items you want to remove from a collection so let's say age and Club again once we perform our request you will see that we have used the only methods to get the name and the age item from the collection now let's have a look at a couple advantages disadvantages and when to use these methods the contains method is particularly useful when you need to check if an item exists in a collection without having to Loop through the entire collection manually now both the accept and only methods are useful when you want to filter a collection based on certain criteria however it's worth noting that these methods can be slower than using a for each Loop in certain situations if you work with a very large collection it may be more efficient to Loop through a collection manually rather than using these methods unfortunately these methods do not work on Largo models or queries because it can only be used on collections so we can simply say well article where the user underscore ID is equal to one except the excerpt and the description and finally get me the values once we perform our request you'll see that I have not pulled in my article class performance again you will see that where you get an undefined method because the accept method does not exist if you want to select specific columns from your query like we have been doing right here you could simply replace the accept method with the select methods apparently the user with user id1 has not created any post so let's change it to 10 where you will see that we have only selected the excerpt and the description of a post I personally find the map method one of the most powerful if not the most powerful collection method that larva has to offer it is great for transforming collections of data it allows you to take an array or a collection of data and apply a function or method to each item returning a new collection with the transform data I have created a simple collection right here with a key value pair of name and position so let's define a new variable right below of it named mapped and let's set it equal to the player's collection and let's change the map method the map method accepts a callback function as its argument so let's pass it in and the Callback function is used to transform the data in the collection the Callback function does accept one argument which represents each item in the collection that's being transformed so we're going to use our players collection with every single item represents one single player now let's see what we can do with one player so let's return the player and let's add the let's say name once we perform our request you'll see that it has returned a new collection where it has only added the name of our three players now let's see if we can add a new key value pair right here which will apply for all players and it's actually very simple since one player is equal to the player variable or we can create a new key value Pair by using our player adding a set of brackets and defining a new key of team and I mean actually align it a little bit all right now let's set it equal to PSG and let's then remove the name inside our return statement before my request where you will see that each element inside our collection has a new key value pair of team which has a value of PSG now let's create one more example but I want to define a relationship this time so let's navigate back to phpstorm and let's open the article model and right below my fillable property I'm gonna Define a new public function user because one article belongs to one user where I'm simply going to return this belongs to and it belongs to the user class now let's type in it to a belongs to you now let's quickly open the user model because we need to define the inverse relationship as well where we're going to define a public function articles since one user can have many articles but we're going to return this has many and it has many articles now let's type into it 2A has many all right now let's navigate back to Tinkerbell for a moment now let's get rid of our mapped variable and let's define a new variable named articles and let's set it equal to the article model and we're going to retrieve all rows from the article stable but we're going to add the data from the user to it so let's say Colin colon with and then the get method inside the web method we're going to pass in the relationship that we just defined which is user once we perform our request you will see that we have loaded all the data from the Articles table but if we scroll down to the bottom right here you will see that we have also loaded all data from the users table so let's see how we could map this with only the data that we need let's go right after the get method and let's chain the map method all right let's align it on the line below then inside umap method we're going to define a callback function where every single article will be set equal to one single article then inside our map function we're going to return an array with the data that we need so let's say return a set of brackets where we're going to define a new key of ID which will be set equal to the article ID the title will be article title let's also add the created underscore at which will be set equal to article created underscore at now let's add the format method to it where the format will be M forward slash D forward slash capital Y then on the line below we're going to add user data so let's say that we want to add a new key of user underscore name which will be equal to our article the user method that we have defined and we want to get the name now let's get the email as well so let's say user underscore email is equal to article user email once we perform our request let me actually make the sidebar a little bit bigger you will see that we have retrieved data from the database but we have mapped it in a way better way we have only set the ID title created underscore at the username and the user email inside one single element but what if you want to transfer my collection to an associative array this is where the map with keys methods comes in handy let's take the article model that we have as an example again where we're going to retrieve all articles along with their author's name and email address to do so we can use the wit method the width method is used for eager loading the related data so let's say that we want to eager load the user model and we can then change the get method to it and also the map with keys method let me make it a little bit smaller now inside the map with keys function we're gonna pass in a callback function and inside the Callback function we basically have to say that every single article we're plucking will be equal to one single article then inside our callback function we can simply return an array where we can then Define an associative array where the key of every item will be our article ID where we then have an associative array of values inside of it so inside the array we could basically say well the title is article title let's say that we have to create it underscore at timestamp which will be article created underscore at and then we're going to change the format method to it because we're going to format it as a m forward slash D forward slash capital Y then on the line below we're gonna map the user underscore name which will come from our article user name I'll also the same thing for the user email so let's say article user email so let's summarize what we have done right here we have used the map with keys method to transform the article collection into an associative array where the article ID is the key and an array of article information as the value quick note we wrap the transformed array in square brackets and pass in the return statement as an array right here this is because the map with keys method expects an associative array as its return value then we said that it needs to accept a callback function which Returns the associative array and one important thing to note is that the key must be unique for each item in the collection which is true because the ID is unique once we perform the request you'll see that we have received an error message because I made a typo right here so let's say article format once we perform the request you'll see that in the right panel we have retrieved an article with a title created at but we have then added a username and user email and you'll see that the key of an associative array has been equal to the ID so one two three and so on now we're going to start off with the pluck method which allows you to retrieve a single column value from a collection of records and this is very useful you need to retrieve a list of values from a collection of records and an example might be our collection of article records this is very useful when you need to retrieve a list of values from a collection of Records so let's say that you only want to retrieve the title of all articles that you have now let's get started let's define a new variable named articles and let's set it equal to our article model and let's then use the plug method now quick note the plug method retrieves the data itself so you don't need to change the get method to it as well so we can get rid of that now the plug method accepts one single argument which is the name of the column that you want to retrieve from the collection of Records so in our case let's say the title once we perform our request and scroll up you will see that we have retrieved a collection but we have only selected the title of all of our records personally I'm a huge fan of using the pluck method because I've run into tons of situations where I don't need to retrieve all articles from a row but simply just one by using the plug method you will improve your query speed because it only retrieves the data that you need the next method that I want to cover is the key buy methods and the key buy method is used to restructure a collection into an associative array using the values of specified columns as keys so let's say that we have a collection again of Articles and we want to restructure it so the articles are keyed by their ID to use the key by Method we first need to retrieve our collection we can do this using the article model where we can then just like we did with the plug method and quick note this isn't the best working example because it's not best practice to retrieve all records and then restructure them but for this example I need to have a collection that I would map which is through the article model where we're going to select all articles now that we have our collection let's use the key buy method to restructure it so let's say articles by ID as equal to our articles object where we're going to chain The Key by Method we do need to pass in one argument which is the column or attribute where you want to key it by so let's say ID and what this does is creating a new associative array from our collection where the keys are the IDE values of each article and the values are the article objects themselves so once we perform our request you'll see right here that we have a new associative array with the ID of 1 with data inside of it and our item number two is right here where you will see that the ID is once again number two which is equal to the actual ID of the article with all the data inside of it we could also change the argument of ID to let's say title which will change the associative array keys of the title article so right here you'll see that one has been replaced with the actual title the first method that I want to cover is the push method which is a built-in function in larval's collections class the push method simply allows you to add one or more elements to the end of a collection and this is useful when you want to append new data to an existing collection without overwriting the original data the power of the push method lies in its flexibility the power of the push method lies in its flexibility because it can accept any type of data including integers strings objects and even other collections it's also pretty cool that you don't need to define the push method multiple times in case you want to add or append multiple elements since it accepts multiple optional arguments allowing you to append multiple elements to the collection in one go now let's say that we want to add two new elements to our current collection a string of laravel and a new collection containing the integer values of 1 2 and 3. we can achieve this by using our collection and chaining the push method to it then we can basically pass in two arguments where the first one is a string of laravel while the second one is an array of value 1 2 and 3. once we perform our request you will see that the push method has appended the elements that we have added to the collection variable now next to the push method larval offers the put method which is also a powerful way to modify collections the put method allows you to set a value on a collection by a given key and this is useful when you want to update an existing value or append a new key value pair to the collection one of the main benefits of the put method is that it automatically handles creating a new key value pair or updating an existing one this means that you don't have to worry about checking if the key exists or manually updating the value now let's use our collection variable again and let's chain the put method to it right now we need you to find two arguments a key and a value so let's say that the key is name and the value is John Doe once we perform our request you will see that the put method added a new key value pair to our collection right at the bottom where where the key is equal to name and the value is equal to John Doe if the key name already exists the put method would update the value so let's say that on the line below we're going to use our collection again and change the put method to it the key will be name again and the value will be John once we perform our request you can see that it has not inserted a new element but it has updated an element based on the key another benefit of the put method is that it's chainable this means that you can add multiple key value pairs to a collection and one line of code so right after the first bit method a less chain another fit method or we're going to say that the H is equal to 33. once we perform our request you'll see that it has appended a key value pair of age and a value of 33. now let's talk about a real life scenario where you might use the putt method let's say that you have a collection of user data and you want to update a specific user information you could use the put method to update their name age or location without having to manually search for data in the collection now I do have to mention that there might be a potential disadvantage of the put methods or well I have to mention it so you are aware of it we have pretty much seen it but the issue is that it can overwrite existing data if you use the same key multiple times so be careful when using this method to avoid accidental overriding important data now next to the put method a larval also offers the give me a moment forget method which allows you to remove an item from a collection so let's say that we want to remove the value of developer right here so what we could do is basically using our collection variable again and chaining the forget methods to it now quick note the forget methods can only remove an item from a collection by its key so let's say that we want to delete key number five once we perform our request you will see that the forget method has removed the element at index 5. one of the main benefits of the forget method is the same as the put method because it allows you to use multiple forget methods this means that you can remove multiple items from a collection in one line of code so let's say that we want to forget number one and let's add number zero as well once we perform our request you'll see that we have deleted the first two values the next method that I want to cover is the pop method and the pop method is a powerful way to remove the last element from a collection now let me repeat myself the last element so let's use our collection variable again and let's change the pop method to it unlike other methods the pop method does not accept any argument because it simply removes the last element from an array now once we perform our request you will see that it has outputted the last value but if we then go on the line below and output our collection again you will see that the key value pair of H and 33 has been removed now one of the main benefits is pretty much what we have already seen if we get rid of our collection variable and set our pop methods equal to a variable so let's say removed perform a request you'll see that it has basically sort the removed element in a variable which we can then use later on so larval gives you the chance to pretty much recover if you have made a mistake now one potential disadvantage of the pop method is that it can only remove the last element from a collection this means that you can't use it to remove elements in the middle of the collection the final method that I want to cover is the shift method and the shift method removes the first element from a collection and returns its value and this is useful when you want to remove an element from a collection without having to manually search for it so let's use our variable collection again where we can change the shift method to it now it simply removes the first value so we don't need to add an argument right here and once we perform our request you can see that the shift method has removed the first element from The Collection where the value apparently was an integer of 3. now just like the pop method you could set it equal to a variable let's say removed which we can then use later on the first method that we're going to cover is the concat methods which is used to merge two or more collections values into a single collection and this can be useful when you want to combine data from multiple sources or tables in your database now let's have a look at an example I currently have two simple collections on my screen collection number one which has a value of Barcelona and London and collection number two which has a value of Amsterdam and Berlin now honestly there isn't really a point of having two separate collections for this so what we could do is defining a new variable name to combined where we're going to add collection 2 into collection number one so let's define collection number one where we can then change the concat method to it we do need to pass in one argument of the collection that we want to concat into collection number one which will be collection number two once we perform our request you can see that we have used the concat method to merge the two collections into a new collection called combined the second method that I want to cover is the zip methods which is used to combine two or more collections by matching their values based on their Index this can be useful when you want to combine data from different collections that have a one-to-one relationship let's change the values of our collections let's start off with collection one and let's add Barcelona right here let's add London LS also add Amsterdam then for collection number two we're going to remove the values as well because we're going to add the corresponding countries so let's say Spain United Kingdom and we have Netherlands let's then Define a new variable right at the bottom named zipped and I'll set it equal to collection number one where we're going to chain this ZIP method to it and we're gonna zip it with collection number two once we perform our request you can see that the zip methods combined the two collections by matching their values based on their index the first value in collection one is combined with the first value of collection number two the second value in collection number one is combined with the second value of collection number two and so on if the two collections have different lengths the resulting collection will have the length of the shorter collection so let's remove let's say the Netherlands from our last collection once we perform our request you'll see that collection number one is all right number two as well and number three has a contrary of null we're going to start off with the collapse method which is a method that allows you to collapse a multi-dimensional collection into a single Dimension this can be used when you're working on a complex data structure and you want to simplify things out I currently have a simple collection on my screen with ordered items and each order has multiple line items now let's say that we only need to get a list of all the items in the orders and we don't want the other crap data that we have into their collection so let's say that we don't want the IDS and situations like these we could use the let me scroll to the bottom collapse methods let's define a new variable named items and let's set it equal to the orders collection where we can then change the plug method to it to extract the items array from each order so let's pass in an argument of items which will basically grab this part of the multi-dimensional array and let's then change the collapse method to it which will merge all those arrays into a single collection of items in the output you will see that the collapse method returned a new collection that is a single dimensional collection where all the items from the original multi-dimension collection have been merged together the second method that I want to cover is useful when you want to split a collection into chunks of smaller collections which can be done through the split methods and this can be useful when you're dealing with large collections of data and you want to break them down in more manageable chunks let's dive into our example let's say that you have a collection of blog posts that you want to display on your website however you don't want to display all those posts at once because that can be overwhelming for your users instead you want to display them in smaller chunks such as five posts per page to achieve this we can use the split method but before we do that let's get rid of our orders array let's define a new variable named post and let's set it equal to the collect method let's then pass in an array another array where the title is equal to post one where the body is equal to let's say test so what I want to do right now is basically duplicating the array that we have inside the collection 10 times give me a moment all right and I want to rename the titles to post 2. 3 4 5 6 7 8 9 and I missed one so let's make 10 as well all right then outside of our collection we're going to define a new variable named chunks which we're gonna set equal to our post collection and then we're going to change the split method to it the split method accepts a single parameter this parameter can be either an integer or a callback function that returns a Boolean value if an integer is passed the collection will be split into chunks of that size if a callback function is being passed in the collection will be split whenever the function returns true in the current scenario let's pass N5 let's quickly comment out the collapse method that we have defined before and once we perform our request you will see that the split method has returned a new collection of collections where it has grouped them in groups of two so we have collection number one which basically has pose number one and two then we have another collection for post three and four another collection for five and six another one for seven and eight and the last one for Post 9 and 10. so from 10 rows it has created five collections where each collection has a total of two posts I'm going to cover two pretty interesting methods that are useful for sorting collections by values which is the sword and sword desk methods I personally find the sort method one of the most useful methods in larva collections class it basically does what the name implies it allows you to sort the collection by its values by default it sourced the values in ascending order let's see how this works I currently have a simple collection on my screen with integer values in random order thus Define a new variable named sorted and let's set it equal to our collection variable and let's then change the sort methods to it once we perform our request you will see that it has sorted out our collection now this method is useful when you need to order the data in your application based on a certain criteria one example might be when you want to sort a list of products by their price or a list of posts by their publication dates the second method that I want to cover is the sort by desk method which is very similar to the sort method but with one key difference it sourced The Collection by its value but in descending order instead of ascending order it works in our current example as well what we simply need to to find a new variable named sorted by desk and let's set it equal to our collection and let's chain the sort desk methods to it once we perform our request you'll basically see that the first value is the highest number while the last value is the lowest number where we're going to cover a couple pretty interesting methods that are useful for sorting collections based on their keys which are the sort by sort by desk sort keys and sort Keys task methods let's start off with the source buy method which is a great way to sort a collection by a given key imagine that we have a collections of users with each user containing a name at age now let's say that we want to sort it based on the age what we can do is basically defining a new variable named sorted setting it equal to our collection variable and chaining the sort by method to it the sort by Method it says one argument which is the key where you want to sort it by which in our case it will be H once we perform our request you will see that the collection has been sorted by the ah key in ascending order now when will this be useful well anytime you need to sort a collection by a specific key this method comes in handy it is also great for sorting collections by nested values the second method that I want to cover is sort by desk which stores a collection in descending order based on a given key it is the opposite of the sort by method which shows the collection in ascending order so let's define a new variable name sorted let's set it equal to our collection variable let's change the sort by desk method and let's pass in an argument of age I have a typo right here let me get rid of that all right once we perform our request you will see that our collection has been ordered and descending order based on the age the third method that I want to cover is the let me scroll down for a moment sort Keys methods as the name applies the sort Keys method allows you to sort a collection based on its keys this can be particularly useful when you want to sort a collection based on the order in which the data has been added so let's define a new variable named sorted let's set it equal to our collection and let's change the sort Keys methods in our current example we're going to sort the collection based on the keys of the element in the collection as a result the sorted variable contains the same user data but sorted by the keys in ascending order this method can be particularly useful when you want to sort a collection based on the order which the data has been added just like the sort by Method we also have a sort Keys desk method which allows you to sort a collection based on its Keys however there's once again one difference because it will be sorted in descending order so if we Define a variable named sorted and set it equal to our collection variable and change the sort Keys desk method you will see that this collection has been sorted in descending order quick summary the sort by method is used to sort a collection by a specific key in ascending order while sort by desk Source the collection in descending order both the sort keys and sort Keys desk are used to sort a collection by its key in ascending or descending order if you do like my content and you want to see more leave this video a thumbs up and if you're new to the Channel please hit the Subscribe button
Info
Channel: Code With Dary
Views: 7,079
Rating: undefined out of 5
Keywords: laravel, laravel full course, learn laravel for beginners, laravel methods, top laravel collection methods, laravel collections methods, laravel top collection methods, collection in laravel, laravel collection methods, collection laravel in hindi, laravel collections, Mastering in Laravel, laravel Eloquent Methods, Eloquent Methods, 44 laravel Essential methods, laravel essential methods, laravel methods for beginners, Laravel Collections & Eloquent Methods, 44 laravel methods
Id: XWKnMU6MnIw
Channel Id: undefined
Length: 85min 35sec (5135 seconds)
Published: Wed Jun 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.