Laravel 8 Collection Get First and Last Item Tutorial

Share this Article
Reading Time: 4 minutes
3,110 Views

Inside this article we will see the concept of Laravel 8 collectionget first and last item. Article contains very classified information about Laravel 8 collection getting first and last item.

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.


Create Laravel Collection

Here, we have created a sample user collection.

<?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.


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 Laravel 8 Collection Get First and Last Item Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5