how to find name john and replace with smith in Laravel from name column of a table

Viewed 36

My String from name Column

john_items.3

I want to fetch all these names that start with the john and replace all of them with smith and update columns like

**john_items.3**

will become

**smith_items.3**
1 Answers
$replacedItems = \App\Models\ModelName::where('name', 'LIKE','john'.'%' )
    ->get()
    ->map(function ($item) {
        $item->name = str_replace('john', 'smith', $item->name);
        $item->save();

        return $item;
    });

// Print all
dd($replacedItems->toArray());
Related