0

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.