Step-by-Step To Get First and Last Item in Laravel 10 Collection

Reading Time: 4 minutes
285 Views

This post will show you how to retrieve the first and last item in Laravel 10 collections. There are many times when working with collections in Laravel that you need to retrieve the first or last piece of a collection for further processing or presentation.

Tutorial contains the concept of Step-by-Step To Get First and Last Item in Laravel Collection.

Article contains a very classified information about the basic concept of getting first and last item in laravel.

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: Step-by-Step Guide To Merge Eloquents in Laravel 10 Collection

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.

Create Laravel Collection

Here, we have considered a sample user collection.

We will work with this demo example.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SampleController extends Controller
{
    public function usersCollection()
    {
        $userCollection = collect([
            [
                'id' => 1,
                'name' => 'User 1',
                'email' => 'user1@gmail.com'
            ],
            [
                'id' => 2,
                'name' => 'User 2',
                'email' => 'user2@gmail.com'
            ],
            [
                'id' => 3,
                'name' => 'User 3',
                'email' => 'user3@gmail.com'
            ],
            [
                'id' => 4,
                'name' => 'User 4',
                'email' => 'user4@gmail.com'
            ]
        ]);
  
        //...
    }
}

Next,

We will use methods of collection which helps us to get first and last user value from this collection.

Read More: How To Get Last Record of Database Table in Laravel 10

Get Collection First Item

To get first item value from collection, we will use first() method.

$first = $collection->first();

Get Collection Last Item

To get last item value from collection, we will use last() method.

$last = $collection->last();

Complete Collection Example – First & Last Item

We will create an user collection. From user collection we will fetch the first and last user value.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SampleController extends Controller
{
    public function usersCollection()
    {
        $userCollection = collect([
            [
                'id' => 1,
                'name' => 'User 1',
                'email' => 'user1@gmail.com'
            ],
            [
                'id' => 2,
                'name' => 'User 2',
                'email' => 'user2@gmail.com'
            ],
            [
                'id' => 3,
                'name' => 'User 3',
                'email' => 'user3@gmail.com'
            ],
            [
                'id' => 4,
                'name' => 'User 4',
                'email' => 'user4@gmail.com'
            ]
        ]);
  
        $first = $collection->first();
      
        $last = $collection->last();
      
        echo "<pre>";
        print_r($first);
        print_r($last);
    }
}

Output

Array
(
    [id] => 1
    [name] => User 1
    [email] => user1@gmail.com
)

Array
(
    [id] => 4
    [name] => User 4
    [email] => user4@gmail.com
)

We hope this article helped you to learn Step-by-Step To Get First and Last Item in Laravel 10 Collection in a very detailed way.

Read More: Inertia Js Authentication in Laravel 10 with Jetstream Tutorial

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.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more