Laravel 7 - 没有正确获取存储映像路径
Laravel 7 - not getting storage image path correctly
我无法获取正确的图像 URL 以使用资产显示在视图中。生成的 URL 需要 storage
,当我使用 asset
函数时,它在其中丢失,我必须在 asset('storage/' . $post->image)
[= 之类的路径中手动添加 storage
28=]
我不明白为什么Laravel不自动添加storage
?
符号链接存储
我使用以下命令创建了存储文件夹的符号链接
php artisan storage:link
Question:
What I am looking for is the storage
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');
}
DB image
列存储路径
posts/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>
存储路径
HTML 来源
正如您在上面的参考资料中看到的那样,要获得正确的 URL 我必须将 storage
添加到 asset('storage/'. $post->image)
使用Storage::url()
函数
<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>
另一种有用的方法是在您的 .env 文件中使用 ASSET_URL,例如:
APP_URL=http://localhost:8000
ASSET_URL = http://localhost:8000/storage
我在我的 APP_URL 上附加了 'storage' 以更改资产辅助函数的默认补丁,然后您可以在您的视图中使用资产函数,例如:
<img src="{{asset($post->image)}}" width="60" height="60" alt="">
希望以上方法能帮到你
我无法获取正确的图像 URL 以使用资产显示在视图中。生成的 URL 需要 storage
,当我使用 asset
函数时,它在其中丢失,我必须在 asset('storage/' . $post->image)
[= 之类的路径中手动添加 storage
28=]
我不明白为什么Laravel不自动添加storage
?
符号链接存储
我使用以下命令创建了存储文件夹的符号链接
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');
}
DB image
列存储路径
posts/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>
存储路径
HTML 来源
正如您在上面的参考资料中看到的那样,要获得正确的 URL 我必须将 storage
添加到 asset('storage/'. $post->image)
使用Storage::url()
函数
<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>
另一种有用的方法是在您的 .env 文件中使用 ASSET_URL,例如:
APP_URL=http://localhost:8000
ASSET_URL = http://localhost:8000/storage
我在我的 APP_URL 上附加了 'storage' 以更改资产辅助函数的默认补丁,然后您可以在您的视图中使用资产函数,例如:
<img src="{{asset($post->image)}}" width="60" height="60" alt="">
希望以上方法能帮到你