How to Find and Replace Elements from a Collection in Laravel?

Nov 01, 2022 . Admin



Hello Friends,

In this example, I will show you how to find and replace elements from a collection in laravel. if you want to see an example of the laravel collection find and replace item example then you are in the right place. This article will give you a simple example of the laravel collection finding items and replacing them. I want to share with you the laravel find and replace the item from the collection. You just need to do some steps to do laravel to find and replace an item in a collection.

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\FindReplaceController;

/*
|--------------------------------------------------------------------------
| 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',[FindReplaceController::class, 'index']);
Step 3: create FindReplaceController

we will create FindReplaceController. so you can see the below code with output:

php artisan make:controller FindReplaceController
App\Http\Controllers\FindReplaceController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FindReplaceController extends Controller
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
    */
    public function index()
    {

        $collection = collect([0 => 'Taylor', 1 => 'Abigail', 2 => 'James', 3 => 'Ganifar']);

        $selector[] = 1;     

        $replaced = $collection->replace([$selector[0] => 'Victoria']);
         
        $replaced->all();

        dd($replaced);

    }
}
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:4 [▼
    0 => "Taylor"
    1 => "Victoria"
    2 => "James"
    3 => "Ganifar"
  ]

I hope it can help you...

#Laravel