简体   繁体   中英

How to display Image saved in storage folder on blade

I would like to display an image saved in storage directory in a blade view. I am succesfully storing the file to the storage/app/public folder like so ProfilesController file:

`if(request('image')) {
            $imagePath = request('image')->store('profile', 'public');

            $image = Image::make(public_path("storage/{$imagePath}"))->fit(1000, 1000);
            $image->save();
        }

        auth()->user()->profile->update(array_merge(
            $data,
            ['image' => $imagePath] 
        ));`

Now I would like to retrieve the image in my index.blade.php file. This is how have done it:

<img src="/storage/{{$user->profile->image}}" alt="">

The laravel documentation says use storage link in my terminal I did this

php artisan storage:link

I tried all that but there is no image loading to the view!

What am I doing wrong and how to fix!

    <img class="" src="{{ Storage::disk('name-option')->url('full path.jpg') }}"  alt="">

disk('name-option') 是您可以跳过的选项

<img class="" src="{{ Storage::url('full path.jpg') }}"  alt="">

您应该在用户配置文件模型中使用访问器,即:-

public function getImageAttribute($image) { return asset('storage'.$image); }

This is how I would solve it. The actual solution is on linking the storage directory with the public folder via the storage:link command as per the documentation enter link description here

if(request('image')) {
       $imageName=time().'.' . $request->file('image')->extension();

        $imagePath = request('image')->storeAs('profile', $imageName);

        //you can use $imagePath to save the name as well as the path of the image
        $image->save();
    }

    ;`

In your filesystems.php file in the config folder, append the profile folder to match with the public folder like this:

'links' => [
    public_path('storage') => storage_path('app/public'),
    public_path('profile') => storage_path('app/profile'),

],

Ensure you re-run the command:

php artisan storage:link

In order to access the image,

<img src="{{asset($user->profile->image)}}" alt="">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM