In this tutorial you will learn about the Laravel 5.8 Multiple Image Upload with jQuery Add More Button and its application with practical example.
Laravel 5.8 Multiple Image Upload with jQuery Add More Button
In this Laravel 5.8 Multiple Image Upload with jquery add more button example, we will learn how to upload multiple image using jquery add more button. In this laravel 5.8 multiple image upload example, I’ll show you how to validate and upload multiple image into folder and then save it into database. In this tutorial, we will use jquery to populate multiple image or file upload field. Before saving multiple image into database we will validate image and then save it into directory. After successfully uploading multiple images into the folder and saving it in database we will display success message on the screen.
Install Laravel 5.8
First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel MultipleImageUpload |
Configure Database In .env file
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.
.env
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=MultipleImageUpload DB_USERNAME=root DB_PASSWORD= |
Generate Migration
Now, we have to define table schema for photos table. Open terminal and let’s run the following command to generate a migration file to create photos table in our database.
1 |
php artisan make:migration create_photos_table |
Once this command is executed you will find a migration file created under “database/migrations”. lets open migration file and put following code in it –
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\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePhotosTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('photos', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('photo_name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('photos'); } } |
Run Migration
Now, run following command to migrate database schema.
1 |
php artisan migrate |
After, the migration executed successfully the photos table will be created in database along with migrations, password_resets and users table.
Create Model
Next, we need to create a model called Photo using below command.
1 |
php artisan make:model Photo |
Once, the above command is executed it will create a model file Photo.php in app directory.
Create Controller
Now, lets create a controller for multiple image uploading. Create a controller named ImageUploadController using command given below –
1 |
php artisan make:controller jquery_multiple_image_upload/ImageUploadController |
Once the above command executed, it will create a controller file ImageUploadController.php in app/Http/Controllers/jquery_multiple_image_upload/ directory. Open the ImageUploadController.php file and put the following code in it.
app/Http/Controllers/jquery_multiple_image_upload/ImageUploadController.php
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 |
<?php namespace App\Http\Controllers\jquery_multiple_image_upload; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Validator,Redirect,Response,File; use App\Photo; class ImageUploadController extends Controller { // public function index() { return view('jquery_multiple_image_upload.index'); } public function store(Request $request) { request()->validate([ 'profileImage.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', ]); if ($files = $request->file('profileImage')) { // Define upload path $destinationPath = public_path('/profile_images/'); // upload path foreach($files as $img) { // Upload Orginal Image $profileImage =$img->getClientOriginalName(); $img->move($destinationPath, $profileImage); // Save In Database $imagemodel= new Photo(); $imagemodel->photo_name="$profileImage"; $imagemodel->save(); } } return back()->with('success', 'Image Upload successfully'); } } |
Here In the controller, we have following methods –
index() :- It displays Image Upload Form along with jQuery Add More Button.
store() :- To Upload and Save Multiple Image in database .
Note:- Before uploading any file make sure you have created following two directory in the public folder called profile_images.
Create Blade / View Files
In this step, we will create view/blade file to display Image Upload Form. Lets create a blade file “index.blade.php” in “resources/views/jquery_multiple_image_upload/” directory and put the following code in it respectively.
resources/views/jquery_multiple_image_upload/index.blade.php
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 |
<html lang="en"> <head> <title>Laravel Multiple Image Upload with Add More Button - W3Adda</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> </head> <body> <div class="container"> @if(session('success')) <div class="alert alert-success"> {{ session('success') }} </div> @endif @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> Some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <h3 class="jumbotron">Laravel Multiple Image Upload with Add More Button - W3Adda</h3> <form method="post" action="{{url('jquery-multiple-image-upload')}}" enctype="multipart/form-data"> @csrf <div class="input-group control-group img_div form-group col-md-4" > <input type="file" name="profileImage[]" class="form-control"> </div> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <button type="submit" class="btn btn-success" style="margin-top:10px">Upload Image</button> </div> </div> </form> </div> </body> </html> |
jQuery Add More Button To File Upload Field Dynamically
Now, we will append a “Add” button to clone and append multiple file upload field to the form. Below is final view file code –
resources/views/jquery_multiple_image_upload/index.blade.php
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<html lang="en"> <head> <title>Laravel Multiple Image Upload with Add More Button - W3Adda</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> </head> <body> <div class="container"> @if(session('success')) <div class="alert alert-success"> {{ session('success') }} </div> @endif @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> Some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <h3 class="jumbotron">Laravel Multiple Image Upload with Add More Button - W3Adda</h3> <form method="post" action="{{url('jquery-multiple-image-upload')}}" enctype="multipart/form-data"> @csrf <div class="input-group control-group img_div form-group col-md-4" > <input type="file" name="profileImage[]" class="form-control"> <!-- Add More Button --> <div class="input-group-btn"> <button class="btn btn-success btn-add-more" type="button"><i class="glyphicon glyphicon-plus"></i>Add</button> </div> <!-- End --> </div> <!-- Add More Image upload field --> <div class="clone hide "> <div class="control-group input-group form-group col-md-4" style="margin-top:10px"> <input type="file" name="profileImage[]" class="form-control"> <div class="input-group-btn"> <button class="btn btn-danger btn-remove" type="button"><i class="glyphicon glyphicon-remove"></i> Remove</button> </div> </div> </div> <!-- End --> <div class="row"> <div class="col-md-4"></div> <div class="form-group col-md-4"> <button type="submit" class="btn btn-success" style="margin-top:10px">Upload Image</button> </div> </div> </form> </div> </body> </html> <script type="text/javascript"> $(document).ready(function() { $(".btn-add-more").click(function(){ var html = $(".clone").html(); $(".img_div").after(html); }); $("body").on("click",".btn-remove",function(){ $(this).parents(".control-group").remove(); }); }); </script> |
Create Routes
After this, we need to add following routes in “routes/web.php” file along with a resource route. Lets open “routes/web.php” file and add following route.
routes/web.php
1 2 |
Route::get('jquery-multiple-image-upload', 'jquery_multiple_image_upload\ImageUploadController@index'); Route::post('jquery-multiple-image-upload', 'jquery_multiple_image_upload\ImageUploadController@store'); |
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 –
http://localhost:8000/jquery-multiple-image-upload
Output:-
After Image Upload Screen Output:-
Click “Add” button to add Multiple Image files to upload control.
Now, upload image.