我有一个产品,其中有多张照片由json保存。
public function store(ProductRequest $request)
{
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name=$image->getClientOriginalName();
$year = Carbon::now()->year;
$month = Carbon::now()->month;
$day = Carbon::now()->day;
$imagePath = "/upload/images/{$year}/{$month}/{$day}";
$image->move(public_path($imagePath), $name);
$data[] = $imagePath . '/' . $name;
}
}
product::create(array_merge($request->all() , ['images' => json_encode($data)]));
$request->session()->flash('success', '!محصول با موفقیت ثبت شد');
return redirect(route('Products.index'));
}
我的产品及其存储方式
#original: array:23 [▼
"id" => 16
"name" => "eqwsa"
"code" => "۱۰۲۶"
"price" => "۱۲۰۰۰۰"
"slug" => "eqwsa"
"description" => null
"images" => "["\/upload\/images\/2020\/6\/15\/index222.jpg","\/upload\/images\/2020\/6\/15\/carpet4-1.jpg"]"
"tags" => "eddsfew"
"viewCount" => 0
"commentCount" => 0
"stockCount" => 0
"sailedCount" => "0"
"specialSail" => 0
"specialPrice" => null
"size_id" => 7
"color_id" => 7
"discount_id" => 7
"yarn_id" => 1
"category_id" => 10
"density_id" => 4
"comb_id" => 5
"created_at" => "2020-06-15 12:04:55"
"updated_at" => "2020-06-15 12:04:55"
]
在我的产品模型中,我指定图像应为数组形式:
protected $casts = [
'images' => 'array',
];
如何在刀片中显示该产品的所有照片?
答案 0 :(得分:0)
这应该非常简单,您只需使用图像标签遍历刀片中的阵列即可。例如:
foreach($product->images as $image) {
<img src="{{$image}} alt="image text" />
}
这假定您已经在视图中做过类似的事情
$product = YourProduct;
return view('product.show')->withProduct($product);
其中view()
参数是刀片的名称。
您可以考虑在与产品相关的表中添加媒体对象,这样就可以保持图像的结构,命名,元标记等。