arrayslaraveldropzone.js

How to upload multiple images using foreach loop in laravel


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();

Solution

  • 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