Laravel 8 Collection first() and firstWhere() Methods

Reading Time: 5 minutes
2,877 Views

Inside this article we will see the use of first() and firstWhere() methods in laravel 8 collections. Article contains a very classified information about the basic concept of Laravel 8 Collection first() and firstWhere().

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.

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]);

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Example #1 Use first() Method

In this example we will consider a multi dimensional array. We will see how to use first() into collection.

Suppose we have SiteController.php a controller file inside /app/Http/Controllers folder.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function index()
    {
        $data = collect([
            ["id" => 1, "name" => "Sanjay", "email" => "sanjay@gmail.com"],
            ["id" => 2, "name" => "Vijay", "email" => "vijay@gmail.com"],
            ["id" => 3, "name" => "Ashish", "email" => "ashish@gmail.com"]
        ]);

        $first = $data->first();

        dd($first);
    }
}

Output

When we execute this code snippet, then we will get something like this –


Example #2 Use firstWhere() Method

We will see how to use firstWhere() into collection.

firstWhere() method indicates that we will use some conditions to get values.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function index()
    {
        $data = collect([
            ["id" => 1, "name" => "Sanjay", "email" => "sanjay@gmail.com"],
            ["id" => 2, "name" => "Vijay", "email" => "vijay@gmail.com"],
            ["id" => 3, "name" => "Ashish", "email" => "ashish@gmail.com"]
        ]);

        $first = $data->firstWhere("email", "vijay@gmail.com");

        dd($first);
    }
}

Condition

$first = $data->firstWhere("email", "vijay@gmail.com");

Output

When we execute this code snippet, then we will get something like this –

Another example for using firstWhere(),

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function index()
    {
        $data = collect([
            ["id" => 1, "name" => "Sanjay", "email" => "sanjay@example.com", "age" => 29],
            ["id" => 2, "name" => "Vijay", "email" => "vijay@gmail.com", "age" => 32],
            ["id" => 3, "name" => "Ashish", "email" => "ashish@sample.com", "age" => 25]
        ]);

        $first = $data->firstWhere("age", ">", 30);

        dd($first);
    }
}

Condition

$first = $data->firstWhere("age", ">", 30);

Output

We hope this article helped you to Laravel 8 Collection first() and firstWhere() Methods Tutorial 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.