Laravel Collection Remove First Item Example

By Hardik Savani November 5, 2023 Category : Laravel

Hello everyone,

In this article, I will provide a comprehensive guide on removing the first element from a Laravel collection. This example will assist you in removing the initial item from a Laravel collection. I will demonstrate the process of removing the first item from a Laravel collection.

Laravel collection provide shift() method to remove first item from collection. shift() will remove first element from laravel collection. so, let's see simple example with code.

In Laravel, the shift() method is used to remove and return the first item (element) from a collection. Collections in Laravel are a powerful way to work with arrays of data, and they provide various methods to manipulate and retrieve data from the collection.

Here's how you can use the shift() method in Laravel:

Example:

you can see the below controller code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$collection = collect([

'One',

'Two',

'Three',

'Four'

]);

$collection->shift();

$collection = $collection->toArray();

dd($collection);

}

}

Output:

Array

(

[0] => Two

[1] => Three

[2] => Four

)

I hope it can help you...

Shares