In this tutorial you will learn about the Laravel 8 Livewire Select2 Dropdown Tutorial Example and its application with practical example.
In this Laravel 8 livewire select2 example tutorial I will show you how to implement livewire select2 dropdown in laravel 8 application. In this tutorial you will learn to implement livewire select2 dropdown in laravel 8. In this article I will share example to implement livewire select2 dropdown in laravel 8 application.
Laravel 8 Livewire Select2 Dropdown Tutorial Example
In this step by step tutorial I will demonstrate you with example how to create livewire select2 dropdown in laravel 8 application. Please follow the instruction given below:
- Step 1: Install Laravel 8 App
- Step 2: Add Database Detail
- Step 3: Install Livewire Package
- Step 4: Create Select2 Component using Artisan
- Step 5: Add Route For Livewire Select2
- Step 6: Create View File
- Step 7: Run Development Server
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: 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 |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 3: 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 4: Create Select2 Component using Artisan
In this step we will create a livewire component for creating a livewire select2 dropdown component using the following command:
1 |
php artisan make:livewire select2 |
This command will create the following components on the following path:
1 2 3 |
app/Http/Livewire/Select2.php resources/views/livewire/select2.blade.php |
Now, go to app/Http/Livewire folder and open Select2.php file. Then put the following code into your Select2.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 |
<?php namespace App\Http\Livewire; use Livewire\Component; class Select2 extends Component { public $selCity = ''; public $cities = [ 'Rajkot', 'Surat', 'Baroda', ]; /** * Write code on Method * * @return response() */ public function render() { return view('livewire.select2')->extends('layouts.app'); } } |
Now, go to resources/views/livewire folder and open select2.blade.php file. Then add the following code into your select2.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 |
<div> <h1>Laravel 8 Livewire Select2 Example</h1> <strong>Select2 Dropdown: {{ $selCity }}</strong> <div wire:ignore> <select class="form-control" id="select2" > <option value="">-- Select City --</option> @foreach($cities as $city) <option value="{{ $city }}">{{ $city }}</option> @endforeach </select> </div> </div> @push('scripts') <script> $(document).ready(function() { $('#select2').select2(); $('#select2').on('change', function (e) { var data = $('#select2').select2("val"); @this.set('selCity', data); }); }); </script> @endpush |
Step 5: Add Route For Livewire Select2
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 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Livewire\Select2; /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('select2', Select2::class); |
Step 6: Create View File
In this step we will create blade view file. Go to resources/views/livewire folder and create one blade view files that name app.blade.php file. Then add the following code into your app.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 |
<!DOCTYPE html> <html> <head> <title>Laravel Livewire Example</title> @livewireStyles <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script> </head> <body> <div class="container"> @yield('content') </div> </body> @livewireScripts @stack('scripts') </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 |
localhost:8000/select2 |