Sharim's avatar

Change image address stored in database.

I'm using voyager admin panel where I upload an image, it stored in storage folder and the address of this image is stored in database something like this http://localhost:8000/storage/homes\October2022\4rL6df8IgvncAVs4hhEZ.jpg.

If I do in my blade style="background-image: url({{asset('storage/'.$content->big_image)}})" like this.

It didn't work because the address of the image stored in laravel is stored with backslash '' instead of forward slash '/'.

In my inspect element, I changed the slashes like this http://localhost:8000/storage/homes/October2022/4rL6df8IgvncAVs4hhEZ.jpg and it worked. But how can I change it through the code.

0 likes
15 replies
Sinnbeck's avatar
  1. Is the full url stored in database or only \October2022\4rL6df8IgvncAVs4hhEZ.jpg.
  2. What is your code for storing them
Sharim's avatar

@Sinnbeck 1. The stored address is homes\October2022\4rL6df8IgvncAVs4hhEZ.jpg. 2. I'm using composer require tcg/voyager admin panel. Its their own functions to stored images in database. I haven't used any external code.

Sinnbeck's avatar

@Sharim Oh ok I assume you actually had some code yourself when using it. Then I am unsure how to help. Maybe someone else is using it and knows a fix

Sharim's avatar

@Sinnbeck It happened only in background-image. If I use it in <img src="">. It worked fine.

Sinnbeck's avatar

@Sharim Yeah well you need to store them with the correct /. Any chance you are on windows and it picks up the path delimiters are \ ?

Sharim's avatar

@Sinnbeck Yeah I'm on windows and it can pick this \ too. Even in the browser, I can open and see the image with same stored address in database. Only in the background-image is a problem.

Sinnbeck's avatar

@Sharim That is correct. But then I dont understand the question? Are you looking for a browser that supports it in background-image ?

A fix for now can be this (or use an accessor)

style="background-image: url({{asset(str_replace('\\', '/', 'storage/'.$content->big_image))}})"

But if I were you I would try getting Voyager to work correctly (I have never used it so I dont know how it works)

Sharim's avatar

@Sinnbeck Actually I don't want to override voyager's controller for this small issue. Here I found something in voyager's Media Controller:

$success = true;
$message = __('voyager::media.success_uploaded_file');
$path = preg_replace('/^public\//', '', $file);

event(new MediaFileAdded($path));

The code you just sent me is what I was looking for and it worked for me. But why I shouldn't use it?

Sinnbeck's avatar

@Sharim Oh I am not asking you to change the voyager source. Just try and find the bug and if possible implement a fix in your own code. But apparently its a known issue that wont be fixed

Sharim's avatar

@Sinnbeck The str_replace is worked for me not ```Voyager::image()````. Maybe because I'm using my own controller to fetch the records. And voyager is not involved in any part to fetch the records. Because I'm using voyager only to store.

Sinnbeck's avatar

@Sharim Ok well if it works then I assume thats fine. I could just see that it was the suggested solution on that thread

Sharim's avatar

@Sinnbeck Actually you're right. I checked the address from inspect element several time and we need to remove storage/ from the address. Now I added the code like this:

style="background-image: url({{Voyager::image($content->big_image)}})"

and it worked.

Sinnbeck's avatar

@Sharim Ah exelent :) Didnt know it pointed to the storage directory already, but good to know

Sharim's avatar

@Sinnbeck Please storage/ from your correct answer so people will not confuse about the code.

Please or to participate in this conversation.