Skip to content

All topics  /  Laravel

How to Remove an Element by Key From a Collection in Laravel?

Laravel ·

Teams applying “How to Remove an Element by Key From a Collection in Laravel?” in a production project can use the full explanation to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

Hello Friends,

Today, I would like to show you how to remove an element by key from a collection in laravel. you can understand the concept of laravel collection removed by key item example. let’s discuss laravel remove by key an item in a collection. you will learn laravel collection to remove an item by key. Let's start with the laravel collection removed by key item code example.

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

web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RemoveByKeyController;
/*
|--------------------------------------------------------------------------
| 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',[RemoveByKeyController::class, 'index']);

Step 3: create RemoveByKeyController we will create RemoveByKeyController. so you can see the below code with output:

php artisan make:controller RemoveByKeyController

App\Http\Controllers\RemoveByKeyController

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class RemoveByKeyController extends Controller
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
    */
    public function index()
    {
        $collection = collect(['a' => 'Doe', 'b' => "John" , 'c' => "Gadas" , 'd' => "Maxy"]);
        $selected = ["b"]; 
        foreach ($collection as $key => $value) {

            
            if ($selected['0'] == $key) {
                $selected = $value;
                $collection->forget($key);
            }
        }

        
        dd($collection);
    }
}

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.

http://localhost:8000/index

Output:

#items: array:3 [▼
    "a" => "Doe"
    "c" => "Gadas"
    "d" => "Maxy"
]