我试图为我的产品上传多个图像,并将这些图像的名称作为数组存储在数据库中。我的代码是这样的-
$images = $request->file('images');
if (isset($images)) {
foreach($images as $image){
$imagename = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
if (!file_exists('uploads/product/images')) {
mkdir('uploads/product/images', 0777, true);
}
$image->move('uploads/product/images', $imagename);
$data[] = $imagename;
}
}else{
$data[] = 'default.png';
}
$product = new Product();
$product->images = json_encode($data);
存储在该images
字段中的数据类似于-
["jeans-2020-08-13-5f352f18b30a4.jpg","jeans-2020-08-13-5f352f18b36a0.jpg","jeans-2020-08-13-5f352f18b3a2c.jpg"]
**问题是我如何分隔此图像名称以在Laravel Blade中显示图像?**
或建议我,如果还有其他方法可以在laravel-6中上传多个图像或多个值
您需要反序列化图像名称,然后遍历它们:
@foreach(json_decode($product->images) ?? [] as $image)
<img src="uploads/product/images/{{ $image }}">
@endforeach
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句