如何使用 laravel 5.4 获取存储在存储目录中的头像 url?

How to get avatar url which are stored at storage directory using laravel 5.4?

我想获取存储目录中存储的头像url,我正在使用laravel 5.4。

实际上我正在使用以下方法上传图片并获得 url。

正在上传图片:

<input type="file" class="form-control" value="{{ old('u_avatar') }}" name="u_avatar" />

在控制器中

$UserModel->u_avatar = $request->file('u_avatar')->store('avatars'); // Uploading Files properly in the storage/app/avatars

获取图像

我尝试了两种获取图像的方法,但都不起作用

第一种方法

在查看文件中

<img src="{{ asset('storage/app') }}/{{ $User->u_avatar }}" class="img-responsive" /> // Not Working

第二种方法 在控制器中

$User = UserModel::find($id);
//For Image
$path =  asset('storage/app/'.$User->u_avatar);// Not working 

我想请您指导我,我的做法是否正确?在这个 reference 这个人在说

storage is not public. You cannot save files there then create web links to them. This thread is about being able to use laravel to intercept the call for the image, get it from private storage and stream the file to the client.

谁能告诉我在 laravel5.4 中存储文件的最佳方式是什么以及如何以正确的方式获取。我想欣赏。谢谢。

storage 目录无法从网络访问,因此您需要创建一个符号链接。

The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.

To create the symbolic link, you may use the storage:link Artisan command:

php artisan storage:link

Of course, once a file has been stored and the symbolic link has been created, you can create a URL to the files using the asset helper:

echo asset('storage/file.txt');

https://laravel.com/docs/5.4/filesystem#the-public-disk