In this tutorial you will learn about the Laravel 7/6 Dropzone Multiple File Upload and its application with practical example.
In this Laravel 7/6 Dropzone Multiple File Upload tutorial, I will show you how to upload multiple image file using dropzone drag and drop in laravel. In this tutorial you will learn to implement drag and drop image upload using dropzone package in laravel. In this tutorial we learn to :-
- Uploading multiple images with dropzone
- Saving images with unique file names to database
Laravel 7/6 Dropzone Multiple File Upload
- Install Laravel App
- Setup Database Credentials in .env
- Create Route
- Generate a Controller & Model
- Create a View File
- Start Development Server
Step 1: Install Laravel App
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel laravelDropzone |
Step 2: Setup Database Credentials in .env
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=databasename DB_USERNAME=db_username DB_PASSWORD=db_password |
Step 3: Create Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 |
Route::get('dropzone/image','ImageController@index'); Route::post('dropzone/store','ImageController@store'); |
Step 4: Generate a Controller & Model
In this step we will generate a controller and model file using following artisan command:
1 |
php artisan make:model Image -mcr |
The above command will generate a Image model, migration file for Image Table and also will create one controller name ImageController.php. Now, go to database/migrations directory and open create_images_table.php file and put the following code in create_images_table.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateImagesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('images', function (Blueprint $table) { $table->id(); $table->string('title'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('images'); } } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Next, Navigate to app/http/controllers direcotry and open ImageController.php. Then update the following methods into your ImageController.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Image; class HomeController extends Controller { public function index() { return view('image'); } public function store(Request $request) { $image = $request->file('file'); $avatarName = $image->getClientOriginalName(); $image->move(public_path('images'),$avatarName); $imageUpload = new Image(); $imageUpload->title = $avatarName; $imageUpload->save(); return response()->json(['success'=>$avatarName]); } } |
Step 5: Create Blade view
In this step, we will create a blade file. So go to /resources/views folder and create one file name image.blade.php. Now, put the following code into your image.blade.php file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Laravel Upload Image Using Dropzone Tutorial</title> <link rel="stylesheet" href="{{asset('css/app.css')}}"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.0/min/dropzone.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.0/dropzone.js"></script> </head> <body> <div class="container"> <h2>Laravel 7/6 Upload Image Using Dropzone Tutorial</h2><br/> <form method="post" action="{{url('dropzone/store')}}" enctype="multipart/form-data" class="dropzone" id="dropzone"> @csrf </form> </div> <script type="text/javascript"> Dropzone.options.dropzone = { maxFilesize: 10, renameFile: function (file) { var dt = new Date(); var time = dt.getTime(); return time + file.name; }, acceptedFiles: ".jpeg,.jpg,.png,.gif", addRemoveLinks: true, timeout: 60000, success: function (file, response) { console.log(response); }, error: function (file, response) { return false; } }; </script> </body> </html> |
In the above view blade file, we necessary to include dropzone js and CSS. Here we are going to use CDN js and CSS of dropzone in laravel.
1 2 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.0/min/dropzone.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.5.0/dropzone.js"></script> |
Step 6: Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 2 3 4 5 |
http://localhost:8000/image Or direct hit in your browser http://localhost/laravelDropzone/public/image |