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

Chad Priddle

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!

haakym

I'm unsure of how you're managing the multiple fields, whether it's set in stone or can dynamically change. I have previously done dynamic approaches with javascript and kept track of how many dynamic fields I required with a hidden text field as a count.

Using this approach your html might look something like this:

<input name="imageAndDescriptionCount" type="hidden" value="2">
<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>

So here we have two image and description fields each and the counter is set at 2.

If we were to submit the form and checkout the request via dd() it would look something like this:

"imageAndDescriptionCount" => "2"
"description" => array:2 [▼
    0 => "description"
    1 => "description"
]
    "image" => array:2 [▼
    0 => UploadedFile {#154 ▶}
    1 => UploadedFile {#155 ▶}
]

This then sets you up nicely for a for loop in your controller:

$count = $request->get('imageAndDescriptionCount');
$description = $request->get('description'); // assign array of descriptions
$image = $request->file('image'); // assign array of images

// set upload path using https://laravel.com/docs/5.1/helpers#method-storage-path 
// make sure 'storage/uploads' exists first
$destinationPath = storage_path . '/uploads'; 

for($i = 0; $i < $count; $i++) {

    $data = new Finding;
    $data->description = [$i];

    $file = $image[$i];

    if ($file->isValid()) {
        $extension = $file->getClientOriginalExtension(); // file extension
        $fileName = uniqid(). '.' .$extension; // file name with extension
        $file->move($destinationPath, $fileName); // move file to our uploads path

        $data->image_location = $fileName;
        // or you could say $destinationPath . '/' . $fileName
        $data->save();
    } else {
        // handle error here
    }

}

flash('success', 'Uploads Successful');
return Redirect::to('/upload');

Please note there is no use of the intervention library in your code to my knowledge and I've just wrote the code to perform the upload. I hope this helps you!

Edit

If you really want to use a foreach loop and you know you'll have a description for each image you can do it like this (but I personally think having a counter is a better approach)

$descriptions = $request->get('description'); // assign array of descriptions
$images = $request->file('image'); // assign array of images

// loop through the descriptions array
foreach($descriptions as $key => $val) {

    // You can access the description like this
    $val
    // or this
    $descriptions[$key]

    // so naturally you can access the image like this:
    $images[$key]

}

You could also instead check the count of the descriptions/images and use that as a counter to loop through:

$descriptions = $request->get('description');
$images = $request->file('image'); // assign array of images

$descCount = count($descriptions);
$imgCount = count($images);

If however you may have a different layout, i.e. you don't have 1 description for 1 image then please clarify as you'd need to take a different approach.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Uploading Images in laravel using Intervention

From Dev

Images upload form laravel

From Dev

Laravel 5 - Image Upload and Resize using Intervention Image Package

From Dev

Laravel collective form and foreach loop

From Dev

Post form data onclick event of button using ajax in foreach loop

From Dev

how to loop images using foreach loop

From Dev

codeigniter form upload with images using ajax

From Dev

Laravel 5 - blurry image after upload - intervention

From Dev

Intervention image multiple image upload in laravel

From Dev

Laravel 5 intervention image upload multiple size

From Dev

caching dynamic images with Laravel Intervention library not working

From Dev

Large images not being uploaded with Intervention in Laravel

From Dev

Displaying images from array using a foreach loop

From Dev

How shall I send an image file in body of Http POST request in along with other form data angular 5. Backend is using Intervention package in Laravel

From Dev

Using Google Scripts to upload without intervention

From Dev

submit form data from a foreach loop

From Dev

Laravel 5.1 - Foreach data in form (Blade)

From Dev

How to decode the json data from WHMCS and display the data using foreach loop in laravel-5.6

From Dev

What exactly are MultiPartEntity and Form Data? How are they used to upload images in android?

From Dev

Laravel Mass update using foreach loop

From Dev

Deleting multiple records in laravel using foreach loop

From Dev

Laravel Mass update using foreach loop

From Dev

Upload File in Spring without using form data

From Dev

Upload File in form data using Java

From Dev

Upload File in Spring without using form data

From Dev

Upload file using form-data body

From Dev

Intervention \ Image \ Exception \ ImageNotWritableException using Laravel 4

From Dev

Intervention \ Image \ Exception \ NotReadableException using laravel 4

From Dev

Laravel 5.2: create image thumbnails in using intervention

Related Related

  1. 1

    Uploading Images in laravel using Intervention

  2. 2

    Images upload form laravel

  3. 3

    Laravel 5 - Image Upload and Resize using Intervention Image Package

  4. 4

    Laravel collective form and foreach loop

  5. 5

    Post form data onclick event of button using ajax in foreach loop

  6. 6

    how to loop images using foreach loop

  7. 7

    codeigniter form upload with images using ajax

  8. 8

    Laravel 5 - blurry image after upload - intervention

  9. 9

    Intervention image multiple image upload in laravel

  10. 10

    Laravel 5 intervention image upload multiple size

  11. 11

    caching dynamic images with Laravel Intervention library not working

  12. 12

    Large images not being uploaded with Intervention in Laravel

  13. 13

    Displaying images from array using a foreach loop

  14. 14

    How shall I send an image file in body of Http POST request in along with other form data angular 5. Backend is using Intervention package in Laravel

  15. 15

    Using Google Scripts to upload without intervention

  16. 16

    submit form data from a foreach loop

  17. 17

    Laravel 5.1 - Foreach data in form (Blade)

  18. 18

    How to decode the json data from WHMCS and display the data using foreach loop in laravel-5.6

  19. 19

    What exactly are MultiPartEntity and Form Data? How are they used to upload images in android?

  20. 20

    Laravel Mass update using foreach loop

  21. 21

    Deleting multiple records in laravel using foreach loop

  22. 22

    Laravel Mass update using foreach loop

  23. 23

    Upload File in Spring without using form data

  24. 24

    Upload File in form data using Java

  25. 25

    Upload File in Spring without using form data

  26. 26

    Upload file using form-data body

  27. 27

    Intervention \ Image \ Exception \ ImageNotWritableException using Laravel 4

  28. 28

    Intervention \ Image \ Exception \ NotReadableException using laravel 4

  29. 29

    Laravel 5.2: create image thumbnails in using intervention

HotTag

Archive