Reputation: 279
I am trying to multiple images insert with Laravel 5.1 using one controller method.
Here is my tried Code:
$images = Input::file('product_images');
foreach ($images as $image) {
$productImage = new ProductImage();
$filename = Input::get('imalatci_id') . '_product' . '_' . date('Y_m_d_H_i_s') . '_' . $image->getClientOriginalName();
Image::make($image->getRealPath())->resize(500, 400)->save('images/products/' . $filename);
$productImage->product_id = $productId;
$productImage->path = 'images/products/' . $filename;
$productImage->save();
}
This code inserts single row to table.
How could gain multiple inserting?
Upvotes: 0
Views: 3997
Reputation: 2112
you can use insert
function for add multiple records in one go.
foreach($images as $image){
$filename = Input::get('imalatci_id') . '_product' . '_' . date('Y_m_d_H_i_s') . '_' . $image->getClientOriginalName();
Image::make($image->getRealPath())->resize(500, 400)->save('images/products/' . $filename);
$image_arr[] = ['product_id'=>$productId,'path'=>'images/products/' . $filename ]; }
ProductImage::insert($image_arr);
but insert function does not update timestamps. You have to update that fields.
Upvotes: 2