Issue
I Trying to upload multiple image for my product, and store the names of that images in database as an array. My code works like this-
$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);
And data stored inside the images
field like-
["jeans-2020-08-13-5f352f18b30a4.jpg","jeans-2020-08-13-5f352f18b36a0.jpg","jeans-2020-08-13-5f352f18b3a2c.jpg"]
**And the problem is how can i separate this image name to show images in Laravel Blade? **
OR Suggest me, If there is another way to upload multiple image or multiple value in laravel-6
Solution
You need to deserialize the images names and then loop through them:
@foreach(json_decode($product->images) ?? [] as $image)
<img src="uploads/product/images/{{ $image }}">
@endforeach
Answered By - Alberto Sinigaglia Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.