In this tutorial you will learn about the Laravel 8 Dynamically Add or Remove Multiple Input Fields using jQuery and its application with practical example.
In this Laravel 8 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 8. In this tutorial you will learn to dynamically add or remove multiple input field in form using jquery in laravel 8. In this tutorial I’ll share example to demonstrate Dynamically Add or Remove multiple Input Fields jQuery.
Laravel 8 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 8. Please follow the instruction given below:
- Step 1: Install Laravel 8 App
- Step 2: Connecting App to Database
- Step 3: Build Migration & Model
- Step 4: Add Routes
- Step 5: Create Controller by Artisan
- Step 6: Create Blade View
- Step 7: Run Development Server
- Step 8: Test App On Browser
Step 1: Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
Step 2: 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 |
Step 3: 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 |
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 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']); |
Step 5: 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.'); } } |
Step 6: 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 8 - 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 8</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> |
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 |