Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lugi's avatar
Level 21

How to save Blade and HTML to DB

I would like to save HTML which contains images to the DB.

Here is example of the text I want to save to DB:

<p>Here is some text.</p>
<img style="width: 401px;" src="img/sHjfo2452cx.png">
<p>Again some text</p>

Now the problem is src because it has to be build dynamically, otherwise it is not working...

Usually I use URL::asset method to correctly fill src:

<img src="{{ URL::asset('img/example.png') }}">

Is it possible to save HTML in DB the way that src contains URL::asset which will then be executed when loaded from DB ? What would you suggest as solution/workaround to this point ?

thanks

0 likes
2 replies
Lugi's avatar
Lugi
OP
Best Answer
Level 21

Actually, here is what I did...

I saved the HTML to DB. Then between loading from DB and passing to the view, I load the HTML into DOM document, get src for all img elements and correct them with URL::route()...so here is part of the code:

foreach($images as $i => $img) {
  $src = $img->getattribute('src');
  $img->removeattribute('src');
  $img->setattribute('src', URL::route($src));
}

Please or to participate in this conversation.