In this tutorial you will learn about the Laravel Add/Remove Multiple Input Fields using jQuery and its application with practical example.
In this Laravel Dynamically Add or Remove multiple Input Fields jQuery tutorial I’ll show you how to dynamically add or remove multiple input field in form using jQuery in laravel. In this tutorial you will learn to dynamically add or remove multiple input field in form using jquery in laravel. In this tutorial I’ll share example to demonstrate Dynamically Add or Remove multiple Input Fields jQuery.
Laravel Add/Remove Multiple Input Fields using jQuery
- Step 1: Install Laravel App
- Step 2: Add Database Details
- Step 3: Create Migration & Model
- Step 4: Add Routes
- Step 5: Create Controller by Artisan
- Step 6: Create Blade View
- Step 7: Run 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 Blog |
Step 2: Add Database Details
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=here your database name here DB_USERNAME=here database username here DB_PASSWORD=here database password here |
Step 3: Create Migration & Model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Todo -m |
The above command will create one model name Todo.php and as well as one migration file for the Todos table.
Now go to database/migrations folder and open create_todos_table.php. Then update the following code into create_todos_table.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 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateTodosTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('todos', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('description'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('todos'); } } |
Then navigate to app/Todo.php file. And the fielable property like following:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Todo extends Model { protected $fillable = [ 'title', 'description' ]; } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 4: 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('add-remove-multiple-input-fields', 'DynamicAddRemoveFieldController@index'); Route::post('add-remove-multiple-input-fields', 'DynamicAddRemoveFieldController@store'); |
Step 5: Generate Controller by Artisan
Now, lets create a controller named DynamicAddRemoveFieldController using command given below –
1 |
php artisan make:controller DynamicAddRemoveFieldController |
Next, Navigate to app/http/controllers/ folder and open DynamicAddRemoveFieldController.php. Then add the following file uploading methods into your DynamicAddRemoveFieldController.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\Todo; use Illuminate\Support\Facades\Validator; class DynamicAddRemoveFieldController extends Controller { public function index() { return view("add-remove-input-fields"); } public function store(Request $request) { $request->validate([ 'moreFields.*.title' => 'required', 'moreFields.*.description' => 'required', ]); foreach ($request->moreFields as $key => $value) { Todo::create($value); } return back()->with('success', 'Todos Has Been Created Successfully.'); } } |
Step 6: Create Blade View
Now, go to resources/views and create one file name add-remove-multiple-input-fields.blade.php Then update the following code into your add-remove-multiple-input-fields.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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
<!DOCTYPE html> <html> <head> <title>Laravel - Add/remove multiple input fields dynamically using Jquery</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/js/bootstrap.min.js"></script> <meta name="csrf-token" content="{{ csrf_token() }}"> </head> <body> <div class="container"> <div class="card mt-3"> <div class="card-header"><h2>Add/remove Multiple Input Todo Fields Dynamically using Jquery</h2></div> <div class="card-body"> <form action="{{ url('add-remove-multiple-input-fields') }}" method="POST"> @csrf @if ($errors->any()) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif @if (Session::has('success')) <div class="alert alert-success text-center"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <p>{{ Session::get('success') }}</p> </div> @endif <table class="table table-bordered" id="dynamicAddRemove"> <tr> <th>Title</th> <th>Description</th> <th>Action</th> </tr> <tr> <td><input type="text" name="moreFields[0][title]" placeholder="Enter title" class="form-control" /></td> <td><input type="text" name="moreFields[0][description]" placeholder="Enter description" class="form-control" /></td> <td><button type="button" name="add" id="add-btn" class="btn btn-success">Add More</button></td> </tr> </table> <button type="submit" class="btn btn-success">Save</button> </form> </div> </div> </div> <script type="text/javascript"> var i = 0; $("#add-btn").click(function(){ ++i; $("#dynamicAddRemove").append('<tr><td><input type="text" name="moreFields['+i+'][title]" placeholder="Enter title" class="form-control" /></td><td><input type="text" name="moreFields['+i+'][description]" placeholder="Enter description" class="form-control" /></td><td><button type="button" class="btn btn-danger remove-tr">Remove</button></td></tr>'); }); $(document).on('click', '.remove-tr', function(){ $(this).parents('tr').remove(); }); </script> </body> </html> |
Step 7: Run 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 |
http://localhost:8000/add-remove-multiple-input-fields |