2

I want to display images in my Laravel Orchid admin panel. Orchid stores the uploaded images in this folder, with the folder name being the date it was uploaded:

storage\app\public\2021\04\21\<name>

Laravel stores the image original name, path, extension etc in a special table called 'attachments'. It is linked to my main table called 'users' by an id, image_id in 'users' and id in 'attachments'. In my database, the path is stored as the date, e.g:

2021/04/21/

I did this SQL join query to retrieve the image:

$image = $user->image_id;
$images = DB::table('users')
          ->leftJoin('attachments', 'users.image_id', '=', 'attachments.id')
          ->where('users.image_id', '=', $image)
          ->get(['name', 'original_name', 'path', 'extension']);
            
            foreach($images as $image)
            {
                
                $image_url = $image->path.$image->name.'.'.$image->extension;
                return "<img src='{$image_url}'
                <span class='small text-muted mt-1 mb-0'>{$image->original_name}</span>";
            }

I can display the original name however the image does not display. I would like to know if there is anything wrong with the way i retrieved the image. Thank you.

12
  • Looks as if the <img tag is missing a closing >, and it might need a size (width and height)? Commented Apr 21, 2021 at 2:46
  • your image tag is mising a > and i think you're missing your project root in your url. it should be something like this: 127.0.0.1:8000/publc/path/to/folder Commented Apr 21, 2021 at 2:46
  • When you inspect the broken link/missing image in the browser, what do you see? An incorrect path or an empty path? Commented Apr 21, 2021 at 2:47
  • @Andrew the path being output is correct, however when I opened it in a new tab, it says the page does not exist. Commented Apr 21, 2021 at 2:54
  • 1
    @BillJustin ok, turns out the link of the picture when i tried it from the browser does not include the /admin part, it is http://127.0.0.1:8000/storage/2021/04/12/12876b4a034032120b0a3cc3972f19cf2411aaaf.png. I kinda need some guidance here. do i need to register the picture route under /admin? Commented Apr 23, 2021 at 2:41

2 Answers 2

1

The public path to your images is configured in your config\filesystems.php file.

There will be a section that looks something like this:

'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

You likely need to correct the image's path you are saving into the database to match what's configured here, note the 'url' value matches what you mentioned it the working location to your photos.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming $image_url="2021/04/21/12876b4a034032120b0a3cc3972f19cf2411aaaf.png" and your file is in /storage/public/2021/04/21/12876b4a034032120b0a3cc3972f19cf2411aaaf.png

As the image is in storage path you can create a route which response the image.

Route::get('/image/{path}', function ($path) {
   return \Illuminate\Support\Facades\Storage::disk('public')->response($path);
})->where('path', '.*')->name('show.image');

And replace your return "<img src='{$image_url}' with return "<img src='{route('show.image',['path'=>$image_url])}'

1 Comment

Image still does not display, and for some reason when I inspect the code, the image source also include the route....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.