Updated April 10, 2023
Definition of Laravel Collection Methods
The Laravel works strong and is effective when it works with arrays. The user can select the collection option effectively when combining Eloquent and Laravel. Eloquent returns the query results, and the result is stored as customized collection objects. The collection class is incorporated from laravel collections and inhibits all the properties given by the laravel collection where all the options are effective. In the article, we can discuss the available collection methods in Laravel, which make the user easy to use.
Introduction to Laravel Collection Methods
The collection methods in laravel incorporate the base collection in laravel, enabling all the available collection methods that cannot be accessed by eloquent collection. The collection object cannot be accessed as customized methods offered by eloquent collection. The syntax to create the simple object in the laravel base collection can be edited in illuminate\ support\ collection class.
The collect helper is used to develop the fresh collection instance from the array, execute the strtoupper function on every component, and eliminate all the null components.
$collection = collect (['course', 'exam', null] ) -> map (function ($name)
{
return upper ($name);
} ) -> reject(function ($name)
{
return empty ($name);
} );
The collection class lets the user link the methods to execute the easy mapping and limit the array. In laravel, the collections are immutable, which insists all the collection methods return every instance of the new collection.
Laravel Collection Methods Listing
The collect helper in laravel executes new files in the below folder, illuminating \ support\ instance collection in the provided array. So the collection in laravel is simple. The output of the eloquent query in Laravel is executed as an instance of the collection.
$collection = collect ([a, b, c]);
The collections are referred to as macroable, enabling the user to include additional methods to the class, executed at runtime. The macro method in the illuminate\support\class accepts the closure, which is returned when the macro function is called. The closure in macros enables access to the collection class, which is executed other than the actual collection class. For example, the below code adds the upper method to the instances of the collection class.
use luminate\ Support\ class Collection;
use luminate\ Support\ Str;
Collection :: macro('Upper', function ()
{
return $this -> map (function ($value)
{
return Str :: upper($value);
});
});
$ collection = collect (['first', 'second']);
$ upper = $collection -> Upper();
// ['A', 'B']
The user should declare the macros collection in the boot technique for the service provider. The macros should accept the exceptional collection class. All the methods to be chained can manipulate the below array. Further, all the method returns the instance for the new collection, enabling the user to prevent the actual copy of the collection array.
$todo collection = collect ([
[
'user_id' => '1',
'title' => 'Laundry',
'brief' => 'have to brief'
],
[
'user_id' => '1',
'title' => 'Assignment',
'brief' => 'finish Maths assignment '
],
] );
This is the simple method to create objects in Laravel.
$ todo = todo :: where ('1', 'user-id') -> get();
Creating Laravel Collection Methods
The collection in laravel stores the object and explains the resulting queries. It is a simple array stored in the collection techniques. However, the collection in Laravel includes additional methods and performs different functions like converting the array, filtering the collection, mapping, and sorting.
1. all(): The collection class in laravel has the unique property called $item, which is comprised of the array that holds all the information. The wrapper collection offers the additional technique to work on the array. If the user wants to use the array without additional quotes, follow the steps below.
$mail = collect ( '[email protected]' '[email protected]') ]) ;
$mail_array = $mails -> all();
Dd ($mails_array);
/* array – 2
*/
2. avg(): The average method is provided by the Laravel collection, which returns the average value. In default, the average of all the values is added to the collection. But the collection is comprised of key => value pair, which is used to give the function and automate the average calculation of all the key values.
$simple _ collection = collect ([2,5,7,35]);
$ s_avg = $ simplex_collection->avg();
// answer
$ key_collection = collect (['score' => 9], ['score' => 8], ['score' => 7], ['score' => 6]);
$key_avg = $key_collection->avg();
// answer
Each()
Every method in collecting laravel is the same to map the array function in JavaScript and PHP. Every method takes every component from the collection and application of the callback function.
3. each(): Every technique in Laravel Collection is similar to the map function on arrays in JavaScript and PHP. It takes every unit element from the collection and implies the callback function. It can apply operations on every element, modify each element, etc.
4. chunk(): It is used to segregate the collection of laravel into small chunks from the actual size.
5. Contains(): It is used to ensure that all the collection of variables holds some value or not. It is easy to check the values passed to the parameter, execute a Boolean function, and display whether it is false or true.
6. count() and countby(): The Count() method provides the total number of objects in a given collection of Laravel.
$collection = collect ( [1,2,3,4,5] );
$collection -> count ();
// 5
7. filter(): It is used to enable the collection of a prior callback function. It returns the collection of filtered components and doesn’t edit the original collection.
8. get(): It is used to execute the value, and the key is passed to all the parameters. If a null value is passed and no value is also returned.
9. search(): It is used to search the collection for the given value and execute the index value.
Conclusion
There are different methods available in the collection class in Laravel. Hence it can be applied whenever required and can be executed accordingly.
Recommended Articles
We hope that this EDUCBA information on “Laravel Collection Methods” was beneficial to you. You can view EDUCBA’s recommended articles for more information.