ItSolutionStuff.com

Laravel Collection Remove First Item Example

By Hardik Savani • November 5, 2023
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...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel Convert Collection to Array Example

Read Now →

Top Recommendations for College Homework

Read Now →

How to Check If Collection is Empty in Laravel?

Read Now →

Laravel Collection map() Add Attribute Example

Read Now →

Laravel Collection Get Unique Values Example

Read Now →

Laravel Collection Sum Column Example

Read Now →

Laravel Collection Check Contains Value Example

Read Now →

Laravel Collection keyBy() Method Example

Read Now →

Laravel Collection intersect() and intersectByKeys() Method Example

Read Now →

Laravel Collection Implode Method Example

Read Now →

Laravel Collection Has Method Example

Read Now →

Laravel Collection Map Method Example

Read Now →

Laravel Collection Push() and Put() Example

Read Now →

Laravel Collection Search Method Example

Read Now →