1 回答
TA贡献1828条经验 获得超3个赞
您可以将图像存储在您的PostController.
PostController在您的(store()方法中)中删除此部分:
if($request->hasFile('image'))
{
$filenameWithExt = $request->file('image')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('image')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$path = $request->file('image')->storeAs('public/image', $fileNameToStore);
}else {
$fileNameToStore = null;
}
并在创建post循环后保存图像。更改此行:
$image->post_id = $request->post_id;
对此:
$image->post_id = $post->id;
首先使用Images顶部的模型PostController。您的新循环应如下所示:
if($request->hasfile('images'))
{
foreach($request->file('images') as $image)
{
$filenameWithExt = $image->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $image->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$path = $image->storeAs('public/image', $fileNameToStore);
$image = new Images;
$image->images = $fileNameToStore;
$image->post_id = $post->id;
$image->save();
}
}
然后(基于模型中的关系)您可以使用如下方式访问图像:
@foreach($post->images as $image)
<img src="public/image{{ $image->fileNameToStore">
@endforeach
- 1 回答
- 0 关注
- 106 浏览
添加回答
举报