简体   繁体   中英

show an uploaded image from storage laravel

i want to see an image that i have uploaded on my rest api.

here is the file i have uploaded

i have try this code,

public function MostrarImagenUsuario($id){
    $usuario = Usuario::find($id);
    $link = Storage::disk('public')->url('app/'.$usuario->foto);
    return response()->file($link);
}

but when i tried to test it with postman it sends me this error.

FileNotFoundException: The file " http://localhost/storage/app/public/IqFviqFca7PPEXQlOK0nnBf11Yg2mXne8xzuR0wh.jpeg " does not exist in file

also i have try this method

public function MostrarImagenUsuario2($id){
    $usuario = Usuario::find($id);
    $link = Storage::get($usuario->foto);;
    return $link;
}

and i get this result:

second result

finally i dont know how to solve this.

your code does not work because you try get image from non public directory in laravel all public asset always store in root public folder. in the case of images you can store in storage/app/public but whwn you want to get image always use public/ folder and it both connected with the sync link.

php artisan storage:link

use this command for sync link

after run this command 1shortcut created in your root public folder. and you can all images from root public shortcut.

$url = Storage::url('database filed name where you store image data');

Make sure you have created storage link by calling php artisan storage:link

Then you can get the url of your storage file.

$url = Storage::url('file.jpg');

Use like this in the model

This function is execute when you get the image property

public function getImageAttribute($value)
{
    if (!$value) {
        return 'http://placehold.it/160x160';
    }

    return env("APP_URL")."/storage/phones/".$value;
}

This function is execute when you set the image property from file input

   public function setImageAttribute($photo)
    {
        $this->attributes['Image'] = move_file($photo, 'phones');
    }

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