Laravel 8 Collection Get First and Last Item Tutorial

Share this Article
Reading Time: 4 minutes
836 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]);

Premium Courses Running Out, Grab Now!

Master the Coding Skills to Become an Expert in Web Development. A lifetime of knowledge at your fingertips.

Enroll in your favourite web development courses risk-free at $4 only.


Learn More –

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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

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.

Find More on Laravel 8 Articles here