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

I am using Laravel 5.1 and would like to have a form with several lines of text fields with their respective image downloads.

the 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 an intervention with the value of $key to save a new row in the table with the corresponding text description when loading the image and still use all the intervention classes? Is my code closed?

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

+5
source share
1 answer

I’m not sure how you manage several fields, regardless of whether it is installed in the stone or can change dynamically. I previously did dynamic approaches with javascript and kept track of how many dynamic fields I need with a hidden text field as an account.

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 of which and the counter are set to 2.

If we were to submit the form and check the request through 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 β–Ά} ] 

Then you are fine-tuning for the for loop in the 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 that your code does not use an intervention library, and I just wrote the code to download. I hope this helps you!

Edit

If you really want to use the foreach loop, and know that you will have a description for each image, you can do it like this (but I personally think that having a counter is the best 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] } 

Instead, you can check the number of descriptions / images and use them as a counter for scrolling:

 $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 do not have 1 description for 1 image, please specify, since you will need to use a different approach.

+4
source

Source: https://habr.com/ru/post/1245098/


All Articles