Laravel Collection Remove First Item Example




Laravel Collection Remove First Item Example

Hello Friends,

In this tutorial, you will learn the laravel collection and remove the first item example. I would like to show you that laravel removes the first item in a collection. we will help you give an example of removing the first element from a collection in laravel. step by step explain the laravel collection's first item remove. So, let's follow a few steps to create an example of laravel removing the first item from the collection.

You can use this example with the versions of laravel 6, laravel 7, laravel 8, and laravel 9.

You have just to follow the below step and you will get the layout as below:

Step 1: Install Laravel

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Format Route



use Illuminate\Support\Facades\Route;

use App\Http\Controllers\RemoveFirstController;



| Web Routes



| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!



Route::get('/index',[RemoveFirstController::class, 'index']);

Step 3: create RemoveFirstController

we will create RemoveFirstController. so you can see the below code with output:

php artisan make:controller RemoveFirstController



namespace App\Http\Controllers;

use Illuminate\Http\Request;

class RemoveFirstController extends Controller



* The attributes that are mass assignable.


* @var array


public function index()


$myCollection = collect(['a','b','c','d','e']);

$removeFirst = $myCollection->shift();




Step 4: Start Development Server

Start the development server. Use the PHP artisan serve command and start your server:

php artisan serve

Now you are ready to run our example so run the below command to quick run.



#items: array:4 [▼

0 => "b"

1 => "c"

2 => "d"

3 => "e"