1 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以將圖像存儲(chǔ)在您的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;
}
并在創(chuàng)建post循環(huán)后保存圖像。更改此行:
$image->post_id = $request->post_id;
對(duì)此:
$image->post_id = $post->id;
首先使用Images頂部的模型PostController。您的新循環(huán)應(yīng)如下所示:
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();
}
}
然后(基于模型中的關(guān)系)您可以使用如下方式訪問圖像:
@foreach($post->images as $image)
<img src="public/image{{ $image->fileNameToStore">
@endforeach
- 1 回答
- 0 關(guān)注
- 136 瀏覽
添加回答
舉報(bào)