Laravel 6 Collection Methods with Examples

Laravel Redirect To Another URL / Web Page - HTTP Laravel Redirects

Today, We want to share with you Laravel 6 Collection Methods with Examples.In this post we will show you laravel collection push with key, hear for laravel collection to array of objects we will give you demo and example for implement.In this post, we will learn about Practical Useful Examples of Using Laravel 6 Lazy Collections Class with an example.

Laravel 6 Collection Methods with Examples

There are the Following The simple About Extend Laravel 6 Eloquent Collection Object Full Information With Example and source code.

Read Also:  Handling Events and Methods using VueJS

As I will cover this Post with live Working example to develop Advanced Laravel 6 Eloquent Collection usage, so the Laravel Collections’ higher order messaging and “when” method in Laravel 6 is used for this example is following below.

Laravel 6 Collection Differnt types of Functions

  • filter()
  • search()
  • chunk()
  • dump()
  • map()
  • zip()
  • whereNotIn()
  • max()
  • pluck()
  • each()
  • tap()
  • pipe()
  • contains()
  • forget()
  • avg()

Awesome Laravel 6 Collection Methods

sample Laravel 6 collection Data

$moviesCollectionData = collect([
    [
        'movie_id' => '1',
        'movie_title' => 'Dabbang 4',
        'slogen' => 'Free download hindi movies',
        'movie_type' => 'hindi'
    ],
    [
        'movie_id' => '2',
        'movie_title' => 'jurasirfparkavanyu',
        'slogen' => 'Free download english movies',
        'movie_type' => 'english'
    ],
    [
        'movie_id' => '3',
        'movie_title' => 'bhatiyabukha',
        'slogen' => 'Tamilrokers new tamil moviews',
        'movie_type' => 'tamil'
    ],
]);

Laravel 6 zip() methods

the values of the given array

$zipped = $moviesCollectionData->zip([1, 2, 3]);
 
$zipped->all();

Laravel 6 whereNotIn() & max() Methods

using whereNotIn()

$moviesCollectionData->whereNotIn('movie_id', [1, 2]);

using max()

$moviesCollectionData->max('movie_id');

pluck():

Laravel 6 pluck() methods

$movie_title = $moviesCollectionData->pluck('movie_title');
$movie_title->all();

$movie_title = $moviesCollectionData->pluck('movie_id', 'movie_title');
$movie_title->all();

each(): iterating over the full collection

in laravel 6 each()

$moviesCollectionData->each(function ($item, $key) {
    info($item['movie_id']);
});

$posts = App\Post::all();
 
$posts->each(function ($item, $key) {
    // Do something
});

$moviesCollectionData->each(function ($item, $key) {
    // Tasks
    if ($key == 1) {
        return false;
    }
});

tap(): tap into the collection at any point

using Laravel 6 tap()

$moviesCollectionData->whereNotIn('movie_id', 3)
    ->tap(function ($moviesCollectionData) {
        $moviesCollectionData = $moviesCollectionData->where('movie_id', 1);
        info($moviesCollectionData->values());
    })
    ->all();

pipe(): similar to the tap method

using Laravel pipe() method

$moviesCollectionData->pipe(function($moviesCollectionData) {
    return $moviesCollectionData->min('movie_id');
});

contains():

using Laravel 6 contains()

$contains = collect(['country' => 'IND', 'state' => 'GB']);
$contains->contains('IND');
// true
 
$contains->contains('SL');
// false

$moviesCollectionData->contains('movie_id', '1');
// true
 
$moviesCollectionData->contains('movie_title', 'Not Found Movie Title');
// false

$moviesCollectionData->contains(function ($value, $key) {
    return strlen($value['movie_title']) < 13;
});
// true

forget(): removes the item from the moviesCollectionData

using Laravel 6 forget()

$forget = collect(['country' => 'usa', 'state' => 'ny']);
 
$forget->forget('country')->all();

avg() : use the average method

laravel 6 avg()

$avg = collect([
    ['products' => 11],
    ['products' => 42],
    ['products' => 8],
    ['products' => 98],
])->avg('products');

$avg = collect([22, 44, 77, 85, 98]);
$avg->avg();

Web Programming Tutorials Example with Demo

Read :

Read Also:  Laravel whereRaw havingRaw and orderByRaw Example

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel add to collection.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

About Pakainfo

I am Jaydeep Gondaliya, a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Pakainfo →

Leave a Reply

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