Laravel 6 Collection Filter Method Examples

Today, We want to share with you Laravel 6 Collection Filter Method.In this post we will show you laravel filter nested collection, hear for laravel 6 collection filter multiple parameters we will give you demo and example for implement.In this post, we will learn about call to a member function filter() on array with an example.

Laravel 6 Collection Filter Method

There are the Following The simple About laravel filter collection by relationship Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 6 Filtering Collection Elements With filter, so the Filtering Eloquent collection data with $collection->filter() is used for this example is following below.

Laravel 6 Collection Methods

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

Solution 1:laravel collection filter multiple parameters

in Laravel 6 Controller

public function index()
{
    $myActors = [
        ['id'=>1, 'name'=>'Virat', 'mark' => 99],
        ['id'=>2, 'name'=>'Aksay', 'mark' => 98],
        ['id'=>3, 'name'=>'Ranveer', 'mark' => 78],
        ['id'=>4, 'name'=>'Salman', 'mark' => 25],
    ];
  
    $myActors = collect($myActors);
   
    $passedPeopeles = $myActors->filter(function ($value, $key) {
        return data_get($value, 'mark') > 78;
    });
   
    $passedPeopeles = $passedPeopeles->all();
   
    dd($passedPeopeles);
}

results

array:2 [▼
  0 => array:3 [▼
    "id" => 1
    "name" => "Virat"
    "mark" => 99
  ]
  3 => array:3 [▼
    "id" => 4
    "name" => "Salman"
    "mark" => 25
  ]
]

Solution 2:call to a member function filter() on array

in Laravel 6 Controller

public function index()
{
    $myActorsId = collect([8, 9, 7, null, false, '', 0, []]);
    $myfilterActorsId = $myActorsId->filter()->all();
    dd($myfilterActorsId);
}

results

array:3 [▼
  0 => 8
  1 => 9
  2 => 7
]

The Laravel 6 filter() function filters the collection using the simple given callback, keeping only those products data that pass a given truth test.

web.php

filter(function ($value, $key) {
        return $value > 41;
    });

    dd($productData->all());
});

Solution 3: Filter() – The Filter The Laravel Collection

Use the filter method to get a list of all the product that price is greater than 1500

public function filter()
{
	$products = Product::all();
	$prods = $products->filter(function ($value, $key) {
		return $value->price > 1500;
	});

	$prods->all();
}
Web Programming Tutorials Example with Demo

Read :

Summary

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

I hope you get an idea about Laravel Collections Filter Method Tutorial With Example.
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.

Leave a Comment