Here you will learn how to remove the last element from a collection in laravel. This article will give you a simple example of the laravel collection’s last item being removed. This article will give you a simple example of laravel removing the last item from the collection.

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

  1. Setup Laravel
composer create-project laravel/laravel example-app
  1. Assign a Route in Web.php
<strong><?php</strong>

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\CollectionRemoveLastController;

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

  1. Create Controller by enter this in terminalCollectionRemoveLastController& now go in that controller to input the code below.
<strong><?php</strong>

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CollectionRemoveLastController extends Controller
{
    <em>/**
</em><em>     * The attributes that are mass assignable.
</em><em>     *
</em><em>     * @var array
</em><em>    */</em>
    public function index()
    {
        $Collection = collect(['x','y','z']);

        $newCollection = $Collection->reverse()->slice(1)->reverse();

        dd($newCollection);
    }
}
  1. Now run your project with this commandphp artisan serveand you will get the result once you are in that route we defined previously.
#items: array:2 [▼
    0 => "x"
    1 => "y"
]

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