Support the ongoing development of →
Views Database Blade
Last updated 9 months ago.


There are multiple ways to do it.

You can use the map function on the collection:

$items = Item::select('name', 'description')->get()->map(static function ($item): Item {
    $item->description = preg_replace('Search', 'Replace', $item->description);
    return $item;
$content = view('items', compact('item'));
$encodeContent = mb_convert_encoding($content, 'Windows-1252', 'auto');

Another option is to use an accessor on your model:

// in your model class
public function getReplacedDescriptionAttribute():string
    return preg_replace('Search', 'Replace', $this->description);

Then you can use replaced_description in your view.

@foreach($items as $item)

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

Tom tklmn Joined 12 Jul 2021


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 - All rights reserved.