Laravel – Dynamically Add or Remove input fields using JQuery

Laravel – Dynamically Add or Remove input fields using JQuery

In this post we will give you information about Laravel – Dynamically Add or Remove input fields using JQuery. Hear we will give you detail about Laravel – Dynamically Add or Remove input fields using JQueryAnd how to use it also give you demo for it if it is necessary.

Today, i am going to share with you how to add more fields using jquery in laravel 5.5 application, i also implemented Dynamically Generated Fields validation, so if you have add dynamically more then one fields with laravel validation then you are at right place.

We may sometime require to generate dynamic add more fields using jquery in your php laravel application. List if you have to add more then one stock with date wise and need to add in single form. So basically you require to add more function that way client can add and remove dynamically their stock with date. So here i make very simple and easy example for dynamic add or remove input fields in laravel 5.5 application.

In this example, i created “tagslist” table and it’s model. I created simple form and there you can add or remove name input fields and click on submit button insert those records in database table. I also implemented dynamic validation of laravel. So just follow bellow step and get full example like as bellow screen shot.


Step 1 : Install Laravel Application

I am going to show you scratch, So we require to get fresh current Laravel 5.5 version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Database Configuration

In this step we have to make database configuration for example database name, username, password etc for our application of laravel 5.5. So let’s open .env file and fill all details like as bellow:





DB_DATABASE=here your database name(blog)

DB_USERNAME=here database username(root)

DB_PASSWORD=here database password(root)

Also see:Laravel 5 Autocomplete using Bootstrap Typeahead JS Example with Demo

Step 3: Create tagslist Table and Model

we are going to create add more dynamic field application for tagslist. so we have to create migration for tagslist table using Laravel 5.5 php artisan command, so first fire bellow command:

php artisan make:migration create_tagslist_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create articles table.


use IlluminateSupportFacadesSchema;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateDatabaseMigrationsMigration;

class CreateTagslistTable extends Migration



* Run the migrations.


* @return void


public function up()


Schema::create('tagslist', function (Blueprint $table) {







* Reverse the migrations.


* @return void


public function down()





Now run migration my following command:

php artisan migrate

After creating table we have to create model for “tagslist” table so just run bellow command and create new model:

php artisan make:model TagList

Ok, so after run bellow command you will find app/TagList.php and put bellow content in TagList.php file:



namespace App;

use IlluminateDatabaseEloquentModel;

class TagList extends Model


public $table = "tagslist";

public $fillable = ['name'];


Step 4: Add Routes

In this is step we need to add two route for add more fields example. so open your routes/web.php file and add following route.




Step 5: Create HomeController

Here, now we should create new controller as HomeController if you haven’t created. So run bellow command and create new controller. bellow controller for create resource controller.

php artisan make:controller HomeController

After bellow command you will find new file in this path app/Http/Controllers/HomeController.php.

In this controller will create seven methods by default as bellow methods:



So, let’s copy bellow code and put on HomeController.php file.



namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppTagList;

use Validator;

class HomeController extends Controller


public function addMore()


return view("addMore");


public function addMorePost(Request $request)


$rules = [];

foreach($request->input('name') as $key => $value) {

$rules["name.{$key}"] = 'required';


$validator = Validator::make($request->all(), $rules);

if ($validator->passes()) {

foreach($request->input('name') as $key => $value) {



return response()->json(['success'=>'done']);


return response()->json(['error'=>$validator->errors()->all()]);



Step 6: Create Blade File

now we move in last step. In this step we have to create just addMore.blade.php blade file.

So let’s just create following file and put bellow code.


<!DOCTYPE html>



<title>Laravel - Dynamically Add or Remove input fields using JQuery</title>

<link rel="stylesheet" href="" />

<script src="//"></script>

<script src=""></script>

<meta name="csrf-token" content="{{ csrf_token() }}">



<div >

<h2 align="center">Laravel - Dynamically Add or Remove input fields using JQuery</h2>

<div >

<form name="add_name" id="add_name">

<div style="display:none">



<div style="display:none">



<div >

<table id="dynamic_field">


<td><input type="text" name="name[]" placeholder="Enter your Name" /></td>

<td><button type="button" name="add" id="add" >Add More</button></td>



<input type="button" name="submit" id="submit" value="Submit" />





<script type="text/javascript">


var postURL = "<?php echo url('addmore'); ?>";

var i=1;



$('#dynamic_field').append('<tr id="row'+i+'" ><td><input type="text" name="name[]" placeholder="Enter your Name" /></td><td><button type="button" name="remove" id="'+i+'" >X</button></td></tr>');


$(document).on('click', '.btn_remove', function(){

var button_id = $(this).attr("id");




headers: {

'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')




















$(".print-success-msg").find("ul").append('<li>Record Inserted Successfully.</li>');





function printErrorMsg (msg) {




$.each( msg, function( key, value ) {








Now you can run this full example using quick run command of laravel like as bellow:

php artisan serve

Now you can open bellow URL on your browser:

Also see:Full Text Search in Laravel 5 Example


I hope it can help you…

Hope this code and post will helped you for implement Laravel – Dynamically Add or Remove input fields using JQuery. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

77  +    =  80

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US