I'm trying to store and show image for my app. Image storing properly but failed to show. First I created symbolic link using command: php artisan storage:link
config/filesystem.php look like:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
to store image:
if($request->hasFile('avatar'))
{
$path = $request->file('avatar')->store('avatars');
//$path=avatars/generated_image_name.extension
$user->avatar=$path;
$user->save();
}
//image storing under: (../storage/app/avatars)
to display image to view:
<img src="storage/{{$user->avatar}}"
alt="no image!!"
class="img-circle"
style="width:150px;height:150px;">
tried src="storage/app/{{$user->avatar}}"
& src="{{$user->avatar}}"
but can't fetch image. Don't understand what I'm doing wrong.
Hate to be that guy, but have you read the docs. You need to create a symbolic link from public/storage
to storage/app/public
.
The Public Disk
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 frompublic/storage
tostorage/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
Again I would check out the docs or maybe read them :).
I think your default storage is local
and not public
. In config/filesystems.php
look at the default driver.
Storing Files > File Uploads > Specifying A Disk
or try something like this.
if($request->hasFile('avatar'))
{
$path = $request->file('avatar')->store(
'avatars/'.$request->user()->id, 'public'
);
//$path=avatars/generated_image_name.extension
$user->avatar=$path;
$user->save();
}