In this tutorial you will learn about the Laravel Livewire Add or Remove Dynamically Input Fields and its application with practical example.
In this Laravel Livewire Add or Remove Dynamically Input Fields tutorial I’ll show you how to add or remove multiple input fields dynamically using livewire in laravel. In this tutorial you will learn to create add or remove multiple input fields dynamically using livewire in laravel.
Laravel Livewire Add or Remove Dynamically Input Fields
- Step 1: Install Laravel App
- Step 2: Add Database Detail
- Step 3: Create Model & Migration using Artisan
- Step 4: Install Livewire Package
- Step 5: Create Component using Artisan
- Step 6: Add Route
- Step 7: Create View File
- Step 8: 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 Detail
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: Run Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Employee -m |
The above command will create a model name Employee.php, and create a migration name create_employees_table.php. Now, go to database/migrations folder and open create_ employees_table.php file. Then put the following code into create_ employees_table.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 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateEmployeesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('employees', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('employees'); } } |
Now, you navigate to app\Providers folder and open AppServiceProvider.php file. And update the following code into AppServiceProvider.php file:
1 2 3 4 5 6 |
use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); } |
Next, open your command prompt and run the following command to create the table into your database:
1 |
php artisan migrate |
Next, Navigate to app/Employee.php and update the following code into your Employee.php model as follow:
1 2 3 4 5 6 7 8 9 10 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Employee extends Model { protected $guarded = []; } |
Step 4: Install Livewire Package
In this step, we will install Livewire Package via the composer dependency manager. Use the following command to install Livewire Package.
1 |
composer require livewire/livewire |
Step 5: Create Component using Artisan
In this step we will create a component using following artisan command:
1 |
php artisan make:livewire employees |
The above command will create a components on the following path:
1 2 |
app/Http/Livewire/Employees.php resources/views/livewire/employees.blade.php |
Now, go to app/Http/Livewire folder and open Employees.php file. Then add the following code into your Employees.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 61 62 63 64 65 66 67 68 |
<?php namespace App\Http\Livewire; use Livewire\Component; use App\Employee; use App\Http\Livewire\Field; use Illuminate\Http\Request; class Employees extends Component { public $employees, $name, $email, $employee_id; public $updateMode = false; public $inputs = []; public $i = 1; public function add($i) { $i = $i + 1; $this->i = $i; array_push($this->inputs ,$i); } public function remove($i) { unset($this->inputs[$i]); } public function render() { $this->employees = Employee::all(); return view('livewire.employees'); } private function resetInputFields(){ $this->name = ''; $this->email = ''; } public function store() { $validatedDate = $this->validate([ 'name.0' => 'required', 'email.0' => 'required', 'name.*' => 'required', 'email.*' => 'required|email', ], [ 'name.0.required' => 'name field is required', 'email.0.required' => 'email field is required', 'email.0.email' => 'The email must be a valid email address.', 'name.*.required' => 'name field is required', 'email.*.required' => 'email field is required', 'email.*.email' => 'The email must be a valid email address.', ] ); foreach ($this->name as $key => $value) { Employee::create(['name' => $this->name[$key], 'email' => $this->email[$key]]); } $this->inputs = []; $this->resetInputFields(); session()->flash('message', 'Employee Has Been Created Successfully.'); } } |
After that, Navigate to resources/views/livewire folder and open employees.blade.php file. Then add the following code into your employees.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 |
<div> @if (session()->has('message')) <div class="alert alert-success"> {{ session('message') }} </div> @endif <form> <div class=" add-input"> <div class="row"> <div class="col-md-5"> <div class="form-group"> <input type="text" class="form-control" placeholder="Enter Name" wire:model="name.0"> @error('name.0') <span class="text-danger error">{{ $message }}</span>@enderror </div> </div> <div class="col-md-5"> <div class="form-group"> <input type="email" class="form-control" wire:model="email.0" placeholder="Enter Email"> @error('email.0') <span class="text-danger error">{{ $message }}</span>@enderror </div> </div> <div class="col-md-2"> <button class="btn text-white btn-info btn-sm" wire:click.prevent="add({{$i}})">Add</button> </div> </div> </div> @foreach($inputs as $key => $value) <div class=" add-input"> <div class="row"> <div class="col-md-5"> <div class="form-group"> <input type="text" class="form-control" placeholder="Enter Name" wire:model="name.{{ $value }}"> @error('name.'.$value) <span class="text-danger error">{{ $message }}</span>@enderror </div> </div> <div class="col-md-5"> <div class="form-group"> <input type="email" class="form-control" wire:model="email.{{ $value }}" placeholder="Enter Email"> @error('email.'.$value) <span class="text-danger error">{{ $message }}</span>@enderror </div> </div> <div class="col-md-2"> <button class="btn btn-danger btn-sm" wire:click.prevent="remove({{$key}})">remove</button> </div> </div> </div> @endforeach <div class="row"> <div class="col-md-12"> <button type="button" wire:click.prevent="store()" class="btn btn-success btn-sm">Save</button> </div> </div> </form> </div> |
Step 6: Add 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 |
Route::get('/employees', function () { return view('home'); }); |
Step 7: Create View File
In this step we will create view file. Go to resources/views/ folder and create one blade view files that name home.blade.php file. Then add the following code into your home.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 61 62 63 64 65 66 67 68 69 70 71 |
<!DOCTYPE html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel Livewire 7 Dynamically Add/Remove Input Fields</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap.min.css"> <!-- Styles --> <style> html, body { background-color: #fff; color: #636b6f; font-family: 'Nunito', sans-serif; font-weight: 200; height: 100vh; margin: 0; } .full-height { height: 100vh; } .flex-center { align-items: center; display: flex; justify-content: center; } .position-ref { position: relative; } .top-right { position: absolute; right: 10px; top: 18px; } .content { text-align: center; } .title { font-size: 84px; } .links > a { color: #636b6f; padding: 0 25px; font-size: 13px; font-weight: 600; letter-spacing: .1rem; text-decoration: none; text-transform: uppercase; } .m-b-md { margin-bottom: 30px; } </style> </head> <body> <div class="container mt-5"> <div class="row mt-5 justify-content-center"> <div class="mt-5 col-md-8"> <div class="card"> <div class="card-header bg-success text-white"><h5 style="font-size: 19px;">Laravel Livewire 7 Dynamically Add/Remove Input Fields</h5></div> <div class="card-body"> @livewire('employees') </div> </div> </div> </div> </div> @livewireScripts </body> </html> |
Step 8: 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/employees |