Reputation: 48
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!
Upvotes: 1
Views: 4279
Reputation: 74
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="">
Upvotes: 0
Reputation: 1
You should use Accessor in the User Profile Model i.e:-
public function getImageAttribute($image) { return asset('storage'.$image); }
Upvotes: 0
Reputation: 1543
<img class="" src="{{ Storage::disk('name-option')->url('full path.jpg') }}" alt="">
disk('name-option') is option you can skip as
<img class="" src="{{ Storage::url('full path.jpg') }}" alt="">
Upvotes: 2