Collections in Laravel offer a robust approach to work with arrays and data in your application. We will walk you through the process of removing the first item from a Laravel 10 Collection in this article. This lesson is intended for all skill levels, whether you are a beginner or an experienced Laravel developer.
Collections are frequently used to handle data sets, and there may be times when you need to remove the initial item for various reasons, such as data rearrangement or processing.
Laravel collection provides shift() method which we can use to do this removal task.
Read More: How To Add Digital Signature To PDF in Laravel 10 Tutorial
Let’s get started.
Laravel Installation
Open terminal and run this command to create a laravel project.
composer create-project laravel/laravel myblog
It will create a project folder with name myblog inside your local system.
To start the development server of laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Example: Remove First Item From Laravel Collection
Here, is an example. Please check it,
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PostController extends Controller { 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
)
That’s it.
We hope this article helped you to learn about Laravel 10 Collection How To Remove First Item Example Tutorial Example in a very detailed way.
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.