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 {
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.