In this tutorial you will learn about the Laravel 9 Dynamically Add or Remove Multiple Input Fields using jQuery and its application with practical example.
In this Laravel 9 Dynamically Add or Remove Multiple Input Fields using jQuery tutorial I’ll show you how to dynamically add or remove multiple input field in form using jQuery in laravel 9. In this tutorial you will learn to dynamically add or remove multiple input field in form using jquery in laravel 9. In this tutorial I’ll share example to demonstrate Dynamically Add or Remove multiple Input Fields jQuery.
Laravel 9 Dynamically Add or Remove Multiple Input Fields using jQuery
In this step by step tutorial we will learn to dynamically add or remove multiple input field in form using jQuery in laravel 9. Please follow the instruction given below:
- Install Laravel 9
- Connecting App to Database
- Build Migration & Model
- Add Routes
- Create Controller by Artisan
- Create Blade View
- Run Development Server
- Test App On Browser
Install Laravel 9
First of all we need to create a fresh laravel project, download and install Laravel 9 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
Connecting App to Database
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 |
Build 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 |
Now, go to database/migrations folder and open create_todos_table.php. Then put the following code into create_todos_table.php:
1 2 3 4 5 6 7 8 |
public function up() { Schema::create('todos', function (Blueprint $table) { $table->id(); $table->string('title'); $table->timestamps(); }); } |
Then open Todo.php file and the fillable property, which is placed on app/models directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Todo extends Model { use HasFactory; protected $fillable = [ 'title' ]; } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
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 3 4 |
use App\Http\Controllers\DynamicAddRemoveFieldController; Route::get('add-remove-multiple-input-fields', [DynamicAddRemoveFieldController::class, 'index']); Route::post('add-remove-multiple-input-fields', [DynamicAddRemoveFieldController::class, 'store']); |
Create Controller by Artisan
Now, lets create a controller named DynamicAddRemoveFieldController using command given below –
1 |
php artisan make:controller DynamicAddRemoveFieldController |
This command will create a controller named DynamicAddRemoveFieldController.php file.
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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\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' ]); foreach ($request->moreFields as $key => $value) { Todo::create($value); } return back()->with('success', 'Todos Has Been Created Successfully.'); } } |
Create Blade View
In this step, we will create a blade view file named add-remove-multiple-input-fields.blade.php. Now, go to resources/views and create one file name add-remove-multiple-input-fields.blade.php Then put 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 |
<!DOCTYPE html> <html> <head> <title>Laravel 9 - 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 In Laravel 9</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>Action</th> </tr> <tr> <td><input type="text" name="moreFields[0][title]" placeholder="Enter title" 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><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> |
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 |