[英]show an uploaded image from storage laravel
我想看看我上傳到我的休息 api 上的圖像。
我試過這個代碼,
public function MostrarImagenUsuario($id){
$usuario = Usuario::find($id);
$link = Storage::disk('public')->url('app/'.$usuario->foto);
return response()->file($link);
}
但是當我嘗試用郵遞員測試它時,它向我發送了這個錯誤。
FileNotFoundException:文件“ http://localhost/storage/app/public/IqFviqFca7PPEXQlOK0nnBf11Yg2mXne8xzuR0wh.jpeg ”在文件中不存在
我也試過這個方法
public function MostrarImagenUsuario2($id){
$usuario = Usuario::find($id);
$link = Storage::get($usuario->foto);;
return $link;
}
我得到了這個結果:
最后我不知道如何解決這個問題。
您的代碼不起作用,因為您嘗試從 laravel 中的非公共目錄獲取圖像,所有公共資產始終存儲在根公共文件夾中。 在圖像的情況下,您可以存儲在 storage/app/public 中,但是您想要獲取圖像始終使用 public/ 文件夾,並且它都與同步鏈接連接。
php artisan storage:link
使用此命令進行同步鏈接
運行此命令后 1shortcut 在您的根公用文件夾中創建。 並且您可以從根公共快捷方式中獲取所有圖像。
$url = Storage::url('database filed name where you store image data');
通過調用 php artisan storage:link 確保您已創建存儲鏈接
然后你可以得到你的存儲文件的 url。
$url = Storage::url('file.jpg');
在模型中像這樣使用
當您獲取圖像屬性時執行此函數
public function getImageAttribute($value)
{
if (!$value) {
return 'http://placehold.it/160x160';
}
return env("APP_URL")."/storage/phones/".$value;
}
當您從文件輸入設置圖像屬性時執行此函數
public function setImageAttribute($photo)
{
$this->attributes['Image'] = move_file($photo, 'phones');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.