fsuuaas's avatar

How to send single email notification after uploading multiple image via Laravel & DropZone?

I'm creating an Image Editing order system, where a user can upload multiple images by DropZone & Laravel. I want to implement after successful image upload image information save to database & send User an email notification. Then redirect to another page with session message. My code sends more than 1 email every time when saving data to the database. But I need to send an email when all image data successfully saved to the database. Here is my Full Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Model\Order;
use Session;
use Mail;
use Illuminate\Support\Facades\Auth;

class Images1Controller extends Controller
{
   
        public function __construct() {
        $this->middleware('auth');
    }
    
     public function doImageUpload(Request $request) {
          //get the file from the post request
          $file = $request->file('file');
          
          //set the  file name
          $filename = $file->getClientOriginalName();
          //move the file to correct location
          $order_number = $request->input('oid');
          $username = Auth::user()->username;
          $path = 'orders/'. $username .'/' . $order_number;
          if(!file_exists($path)){
              mkdir($path, 0777, true);
          }
          
          $file->move($path, $filename);
          
          //save the multiple/single file details to database
          $order = Order::find($request->input('order_id'));
            $order->images()->create([
              'order_id' => $request->input('order_id'),
              'file_name' => $filename,
              'file_size' => $file->getClientSize(),
              'file_mime' => $file->getClientMimeType(),
              'file_path' => $path . '/' . $filename,
              ]);

             Session::put('success','Your order has been submitted successfully.');
             $data = array(
            'fullname' => $order->user->fullname,
            'email' => $order->user->email,
            'order_number' => $order->order_number
        );
        Mail::send('emails.order-submit', $data, function($message) use ($data) {
            $message->from('[email protected]');
            $message->to($data['email']);
            $message->subject('Order Submit');
        });


           
    }
}

DropZone Configuration Code:

    Dropzone.options.uploadImages = {
    autoProcessQueue: false,
    maxFilesize: 2048,
    uploadMultiple: false,
    parallelUploads: 100,
    acceptedFiles: 'image/*',
    addRemoveLinks: true,
    dictRemoveFile: 'Remove',
    
    init: function() {
    var submitButton = document.querySelector("#submit-images")
        addImages = this; // closure

    submitButton.addEventListener("click", function(e) {
        e.preventDefault();
        e.stopPropagation();         
      addImages.processQueue(); // Tell Dropzone to process all queued files.
    });
    this.on("addedfile", function(file) {
      // Show submit button here and/or inform user to click it.
    });
    
    this.on("success", function() {
         if (this.getUploadingFiles().length === 0 && this.getQueuedFiles().length === 0) {
        window.location.href = 'http://localhost:8000/user/orders/manage/';
      }
    });
    }
    };
0 likes
0 replies

Please or to participate in this conversation.