Reputation: 8378
I am unable to fetch the correct image URL to display in view using the asset. The generated URL required storage
which is missing in it when I use the asset
function and I have to manually add storage
in the path like asset('storage/' . $post->image)
I don't understand why the Laravel doesn't add storage
automatically?
I have created a symlink of storage folder using the following command
php artisan storage:link
Question:
What I am looking for is thestorage
folder should be dynamically added to the path so I have to pass only$post->image
.
public function store(PostRequest $request)
{
// upload the image to storage
$image = $request->image->store('posts', 'public');
// create the post
Post::create([
'title' => $request->title,
'description' => $request->description,
'content' => $request->content,
'image' => $image,
]);
// redirect with flash message
return redirect(route('posts.index'))->with('success', 'Post is created');
}
image
Column Stored Pathposts/ibexiCvUvbPKxzOLSMHQKPpDq7eZXrFA0stBoPfw.jpeg
<tbody>
@foreach($posts as $post)
<tr>
<td>
<img src="{{asset($post->image)}}" width="60" height="60" alt="">
</td>
<td>{{ $post->title }}</td>
</tr>
@endforeach
</tbody>
As you can see in the above references to get the correct URL I have to add storage
into asset('storage/'. $post->image)
Upvotes: 1
Views: 4265
Reputation: 46
another way that can be helpful is using ASSET_URL in your .env file such as:
APP_URL=http://localhost:8000
ASSET_URL = http://localhost:8000/storage
I appended 'storage' on my APP_URL to change default patch of asset helper function, then you can use asset function into your view such as this:
<img src="{{asset($post->image)}}" width="60" height="60" alt="">
I hope this way can help you.
Upvotes: 0
Reputation: 12401
use Storage::url()
function
<tbody>
@foreach($posts as $post)
<tr>
<td>
<img src="{{ Storage::url($post->image)}}" width="60" height="60" alt="">
</td>
<td>{{ $post->title }}</td>
</tr>
@endforeach
</tbody>
ref link https://laravel.com/docs/7.x/filesystem#file-urls
Upvotes: 4