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.

Read Also:  Vue watch two dimensional Array and Object

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.

Read Also:  Laravel 7/6 Pluck Example | How to use pluck() in Laravel 6

web.php

<?php
Route::get('/', function () {
    $p_ids = collect([22, 41, 58, 47]);

    $productData = $p_ids->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 :

Read Also:  DataTables Pagination Server-side Processing using PHP and MySQL

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel Eloquent Extend Collection Object
  2. Read Also:  How To Hack WIFI Password Using jumpstart and dumpper
  3. Read Also:  Laravel Search Multiple Columns
  4. Read Also:  JavaScript Redirect URL Example
  5. Read Also:  NodeJS RESTful API User Authentication with AngularJS using PHP MYSQL
  6. Read Also:  Group By Preserve Key using Laravel Collection
  7. Read Also:  Laravel 6 LIKE query example using Eloquent WHERE clause
  8. Read Also:  Laravel 6 Eloquent WHERE Like query Example
  9. Read Also:  Laravel 6 Collection Methods with Examples
  10. Read Also:  PHP Download Images From URL Using cURL Example
CLOSEX