am uploading multiple images to the database using Dropzone.js.am able to send the data to the controller in the function, but the images are sent as single images, and am able to upload the single image to the DB perfectly. I want to upload the images in batches using the foreach loop because I want to resize them using image intervention. how can I convert the images into an array?
this is my dropzone form in the view
<form id="dropzone-form" action="{{ url('admin/alternateimages/'.$rentaldata->id) }}" class="dropzone form-horizontal" role="form" method="POST" enctype="multipart/form-data">
@csrf
</form>
this is the dropzone script
Dropzone.autoDiscover = false;
var dzonerentalimages = new Dropzone("#dropzone-form",{
maxFilesize: 2,
maxFiles: 3,
acceptedFiles: ".jpeg,.jpg,.png",
});
dzonerentalimages.on("success",function(file,response){
console.log(response.message)
if(response.success == 1)
{
alertify.set('notifier','position', 'top-right');
alertify.success(response.message);
alternateimagestable.ajax.reload();
}
});
i tried this function in the controller but am getting data for each image but I want to get the images an array so that i can upload them using foreach loop.how can i achieve this?
public function alternateimages(Request $request,$id)
{
$rentaldata=Rental_house::with('rentalalternateimages')->select('id','rental_name','location_id','rental_image')->find($id);
$data = array();
$validator=Validator::make($request->all(),
[
'file'=>'required|mimes:png,jpg,jpeg|max:2048'
]);
if($validator->fails()){
$data['success']=0;
$data['error']=$validator->errors()->first('file');
}else{
$images=$request->file('file');
dd($images);die();
2
Answers
I’m not too familiar with dropzone but any laravel form input should work the same way. If you want an array in the controller to iterate over, you need to make the
name
value of your input an array. For instance, if you have several files that are being uploaded, in each input you would make the name be something like this:name="my_files[]"
. Notice the brackets within the name attribute. Then in your controller you can iterate over$request->my_files
as an array.The default behaviour is that Dropzone sends images one by one.
You need to not auto process the queue, and instead have the images sent along with the form data.
Follow this Dropzone guide;
https://docs.dropzone.dev/configuration/tutorials/combine-form-data-with-files