Important Useful Laravel Collection Methods with Example

In this laravel collection methods tutorial, we will discuss about laravel collection methods and it’s uses with simple and easy example.

We will illustrate some of the important and useful laravel collection methods. You are developing a application or web using laravel this methods is very useful for us.

By default Laravel return eloquent collection. The laravel collection methods are very simple and simple. We can use laraval collection methods and filter data accordingly. Laravel Collections contains many useful and powerful methods that help to filter records.

When we apply helper methods on eloquent collections. It does not run query the database. Get all the result from database. Than we use laravel collection methods to filter and modify them without run any query to the database.

Contents

  • pluck()
  • chunk()
  • whereNotIn()
  • avg()
  • first()
  • last()
  • merge()
  • max()
  • min()
  • filter()
  • search()
  • sum()

Pluck()

pluck() methods return specific key values in collections :

$collection = collect([
['name' => 'test', 'email' => '[email protected]'],
['name' => 'test1', 'email' => '[email protected]'],
]);
$plucked = $collection->pluck('name');
$plucked->all();

// Output (Response)

['test', 'test']

Chunk()

Chunk method is used to split the collection into multiple into array.

 $tut = collect([a, b, c, d, e, f, g, h]);
$tut = $tut->chunk(3)->toArray();
// Output (Response)
[
0 => [
0 => a,
1 => b,
2 => c
],
1 => [
3 => d,
4 => e,
5 => f
],
2 => [
6 => g,
7 => h
]
]

whereNotIn()

We can use whereNotIn method to filter the collection by a key value not contained within the given array.

 $collection->whereNotIn('product_id', [1, 2]);

The above statement return response leave product_id 1,2. This is just opposite of wherein() method.

avg()

AVG method returns the average value. In this below collection avg method return to average of all numbers.

 $avg = collect([10, 20, 50, 30, 40]);
$avg->avg();
//Output (Response)
30

first()

first() method returns the first value of collection.

$first = collect([1, 2, 3, 4, 5]);
$first->first();
// Output (Response)
1

last()

last() method returns the last value of collection. last method, just opposite of first method.

 $last = collect([1, 2, 3, 4, 5]);
$last->last();
// Output (Response)
5

merge()

merge() methods is used to merge two or more collection or array.

 $collection = collect(['1', '2']);
$merged = $collection->merge(['3', '5']);
$merged->all();
// Output (Response)
[1, 2, 3, 5]

max()

max() method return the maximum value of given collection.

 $max= collect(['1', '2', '3', '4', '5', '6']);
$max->max();
// Output (Response)
6

min()

min() methods return the minimum value of given collection.

 $min= collect(['1', '2', '3', '4', '5', '6']);
$min->min();
// Output (Response)
1

filter()

filter() methods is used to filter the record from collection.

 $collection = collect(['1', '2', '3', '4', '5', '6', '7']);
$filtered = $collection->filter(function ($value, $key) {
return $value > 3;
});
// Output (Response)
[4, 5, 6, 7]

search()

search() method is used to search the collection for a given value. If the value is exist in the collection, return true. If the value does not exist in collection, return false.

 $collection = collect(['a', 'b', 'c', 'd', 'e', 'f', 'h']);
$search = $collection->search(d);
// Output (Response)
4

sum()

sum() method is very simple in collection and it is used to sum of the collection values.

 $collection = collect(['1', '2', '3', '4', '5', '6', '7']);
$sum = $collection->sum();
// Output (Response)
28

toArray()

toArray() methods is used to convert collection into simple php array()

 $collection = collect(['name' => 'test', 'email' => [email protected], 'mobile' => '9876543210']);
$collection->toArray();

// Output (Response)
[
[
'name' => 'test',
'email' => [email protected],
'mobile' => '9876543210'
]
]

Read more about laravel collections methods click here

Spread the love

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *