I was trying three images in three different fields in database..for that I was following method in controller.But the problem I'm facing is though i have three input fields it stored only two images with same name in the public folder and in database it stored value like 1 (for product_image), 4 ( for feature_image), 9 (for slurp_image)
. How do i properly stored the images in public folder as well as database..thanks in advance .
Here is the controller:
public function store(Request $request)
{
$product = new Product();
$files= [];
if($request->file('product_image')) $files[] = $request->file('product_image');
if($request->file('feature_image')) $files[] = $request->file('feature_image');
if($request->file('slurp_image')) $files[] = $request->file('slurp_image');
foreach($files as $file)
{
if(!empty($file))
{
$filename = time().'.'.$file->getClientOriginalExtension();
$file->move('images/',$filename);
}
}
$product->product_image = $filename[0];
$product->feature_image = $filename[1];
$product->slurp_image = $filename[2];
$product->save();
}
here is the View part :
<div class="form-group">
<label>Product Image</label>
<input type="file" name="product_image" id="product_image" width="200px">
</div>
<div class="form-group">
<label>Feature Image</label>
<input type="file" name="feature_image" id="feature_image" width="200px">
</div>
<div class="form-group">
<label>Slurp Image</label>
<input type="file" name="slurp_image" id="slurp_image" width="200px">
</div>
via User57