DEV Community

Cover image for Laravel 8 Collection with first() and firstWhere() Methods Example
Code And Deploy
Code And Deploy

Posted on

3 2

Laravel 8 Collection with first() and firstWhere() Methods Example

Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/laravel-8-collection-with-first-and-firstwhere-methods-example

In this example, I will show you how to implement the Laravel 8 Collection with first() and firstWhere() methods. When doing a Laravel project we should use Laravel collection for easier to process our array data. We will use the collect helper function of Laravel to create a new collection instance from our array.

And sometimes you need to get the first element or with a condition query that will display the first result of our Laravel collection. With the use of first() and firstWhere() methods will make it easier for us.

Example 1: Laravel Collection first() Method Example

public function index()
{
    $collection = collect([
        ['id'=> 1, 'name'=>'Juan Dela Cruz', 'age' => 25],
        ['id'=> 2, 'name'=>'Juana Vasquez', 'age' => 31],
        ['id'=> 3, 'name'=>'Jason Reyes', 'age' => 27],
        ['id'=> 4, 'name'=>'Jake Ramos', 'age' => 43],
    ]);

    $first = $collection->first();

    dd($first);
}
Enter fullscreen mode Exit fullscreen mode

Output:

array:3 [
  "id" => 1
  "name" => "Juan Dela Cruz"
  "age" => 25
]
Enter fullscreen mode Exit fullscreen mode

Example 2: Laravel Collection first() Method Example

public function index()
{
    $collection = collect([
        ['id'=> 1, 'name'=>'Juan Dela Cruz', 'age' => 25, 'gender' => 'Male'],
        ['id'=> 2, 'name'=>'Juana Vasquez', 'age' => 31, 'gender' => 'Female'],
        ['id'=> 3, 'name'=>'Jason Reyes', 'age' => 27, 'gender' => 'Male'],
        ['id'=> 4, 'name'=>'Jake Ramos', 'age' => 43, 'gender' => 'Male'],
    ]);

    $first = $collection->firstWhere('name', 'Juan Dela Cruz');

    dd($first);
}
Enter fullscreen mode Exit fullscreen mode

Output:

laravel-8-collection-with-first

That's it I hope you learn how to get the first element of the Laravel collection using first() and firstWhere() methods.

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-8-collection-with-first-and-firstwhere-methods-example if you want to download this code.

Happy coding :)

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay