ItSolutionStuff.com

Laravel Collection Search Method Example

By Hardik Savani • April 16, 2024
Laravel

This article will help you how to search with collection object in laravel application. i will explain you how to use laravel collection search method. you can easily search with multidimensional array, by key and by value using search method of laravel collection. you can easily use with laravel 5, laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

Laravel Collection object provide several methods that will help to write your own logic. here we will learn how to use search method of laravel collection.

You can use collection search like as bellow syntax:

search()

search(function( 'You can write logic here' ))

Now we will see both examples bellow:

Example 1:

public function index()

{

$myArray = [34, 33, 36, 37];

$myArray = collect($myArray);

$result = $myArray->search(33);

dd($result);

}

Output:

1

Example 2:

public function index()

{

$myArray = [34, 33, 36, 37];

$myArray = collect($myArray);

$result = $myArray->search(function ($value, $key) {

return $value > 34;

});

dd($result);

}

Output:

2

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Create Word Document File in Laravel?

Read Now →

Laravel Scout Algolia Search Example

Read Now →

Laravel Typeahead Search Example Tutorial

Read Now →

Laravel Multiple Where Condition Example

Read Now →

How to use Soft Delete in Laravel?

Read Now →

Laravel 8/7 Paginate with Collection or Array

Read Now →

Laravel Event Broadcasting with Socket.io and Redis Example

Read Now →

How to Send Email Notification in Laravel?

Read Now →

Merge Multiple Collection Paginate in Laravel

Read Now →