Laravel: Upload form data with images in a foreach loop using Intervention

Viewed 7350

I'm using Laravel 5.1 and would like to have a form with a few rows of text fields with their corresponding image uploads.

Form:

<div id="row">
    <input name="description[]" type="text">
    <input name="image[]" type="file">
</div>
<div id="row">
    <input name="description[]" type="text">
    <input name="image[]" type="file">
</div>

Controller:

foreach($request->description as $key => $val){

if($val != null){
    $data = new Finding;
    $data->description = $val; //save description for each loop

    $file = array('image' => Input::file('image'));

    if (Input::file('image')->isValid()) {

        $destinationPath = 'uploads';
        $extension = Input::file('image')->getClientOriginalExtension();
        $fileName = rand(1000,1000).'.'.$extension;
        Input::file('image')->move($destinationPath, $fileName);
        $path = Input::file('image')->getRealPath();

        $data->image_location = $fileName[$key]; //save filename location to db
        $data->save();

        flash('success', 'Uploaded Successful');
        return Redirect::to('/upload');
     } else {
        flash('error', 'Uploaded File Is Not Valid');
        return Redirect::to('/upload');
    }
}

My question is, how do I use intervention with the $key value to save a new row in the table with the associated text description with the image upload and still use all the intervention classes? Is my code close?

I can easily do all this with just one form input with one image upload but my goal is to have a page with multiple rows with inputs. Thanks!

1 Answers
Related