Support the ongoing development of Laravel.io →
Views Database Blade
Last updated 9 months ago.
0
moderator

Hello,

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)
"{!!$item->name!!}";"{!!$item->replaced_description!!}";
@endforeach
0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

Tom tklmn Joined 12 Jul 2021

Moderators

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

Your logo here?

Laravel.io

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

© 2023 Laravel.io - All rights reserved.