This tutorial explains how you use the powerful filter() method in Laravel 10 collections. Working with data is at the heart of web development, as a Laravel developer you understand. When it comes to quickly retrieving and modifying data within collections, the filter() method opens up a world of options.
You will see the concept of Step-by-step guide to using Laravel 10 Collection filter() method.
The Illuminate\Support\Collection
class provides a fluent, convenient wrapper for working with arrays of data. For example, check out the following code. We’ll use the collect
helper to create a new collection instance from the array.
Read More: How To Get Unique values in Laravel 10 Collection Tutorial
As mentioned above, the collect
helper returns a new Illuminate\Support\Collection
instance for the given array.
So, creating a collection is as simple as:
$collection = collect([1, 2, 3]);
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Laravel Collection – filter() Method
We will see how to use filter() into collection.
Suppose we have SiteController.php a controller file inside /app/Http/Controllers folder.
Read More: Step-by-Step Guide To Merge Eloquents in Laravel 10 Collection
Code Example #1
Laravel Collection filter() with multi dimensional array.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SiteController extends Controller { public function index() { $students = collect([ ["id" => 1, "name" => "Sanjay", "email" => "sanjay@gmail.com", "marks" => 88], ["id" => 2, "name" => "Vijay", "email" => "vijay@gmail.com", "marks" => 70], ["id" => 3, "name" => "Ashish", "email" => "ashish@gmail.com", "marks" => 75] ]); $passed = $students->filter(function ($value, $key) { return data_get($value, 'marks') > 70; }); $passed = $passed->all(); dd($passed); } }
Concept
$passed = $students->filter(function ($value, $key) {
return data_get($value, 'marks') > 70;
});
Output
Code Example #2
Laravel Collection filter() with single dimensional array.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class SiteController extends Controller { public function index() { $data = collect([2, 3, 5, null, false, '', 0, []]); $data = $data->filter()->all(); dd($data); } }
Read More: How To Get Last Record of Database Table in Laravel 10
In this case it will remove the false values from array
We hope this article helped you to Learn Laravel 10 Collection filter() Tutorial for Beginners in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.