Laravel Livewire Dependant Dropdown Example

By Hardik Savani April 16, 2024 Category : Laravel

Now, let's see example of laravel livewire dependant dropdown. This article will give you simple example of laravel livewire state city dropdown. This tutorial will give you simple example of how to add dependent drop down list in laravel livewire. step by step explain livewire dependent dropdown in laravel. Let's see bellow example livewire laravel dependent dropdown.

In this tutorial, we will create example of city and state dependant dropdown using laravel livewire. you can use easily add livewire dependant dropdown laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.

So, let's follow bellow step and you will get bellow layout:

Step 1: Install Laravel 8

first of all we need to get fresh Laravel 8 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: Create Migration and Model

In this step, we need to create states and cities table with model. so let's create as bellow:

php artisan make:migration create_states_cities_tables

database/migrations/2021_02_02_143326_create_states_cities_tables.php

<?php

use Illuminate\Database\Migrations\Migration;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Support\Facades\Schema;

class CreateStatesCitiesTables extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

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

$table->id();

$table->string('name');

$table->timestamps();

});

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

$table->id();

$table->integer('state_id');

$table->string('name');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

public function down()

{

Schema::dropIfExists('states');

Schema::dropIfExists('cities');

}

}

now let's create model by using bellow commands:

php artisan make:model State

php artisan make:model City

app/Models/State.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class State extends Model

{

use HasFactory;

protected $fillable = ['name'];

}

app/Models/City.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

class City extends Model

{

use HasFactory;

protected $fillable = ['state_id', 'name'];

}

Step 3: Install Livewire

now in this step, we will simply install livewire to our laravel 8 application using bellow command:

composer require livewire/livewire

Step 4: Create Component

Now here we will create livewire component using their command. so run bellow command to create statecitydropdown component.

php artisan make:livewire statecitydropdown

Now they created fies on both path:

app/Http/Livewire/Statecitydropdown.php

resources/views/livewire/statecitydropdown.blade.php

Now both file we will update as bellow for our contact us form.

app/Http/Livewire/Statecitydropdown.php

<?php

namespace App\Http\Livewire;

use Livewire\Component;

use App\Models\City;

use App\Models\State;

class Statecitydropdown extends Component

{

public $states;

public $cities;

public $selectedState = NULL;

/**

* Write code on Method

*

* @return response()

*/

public function mount()

{

$this->states = State::all();

$this->cities = collect();

}

/**

* Write code on Method

*

* @return response()

*/

public function render()

{

return view('livewire.statecitydropdown')->extends('layouts.app');

}

/**

* Write code on Method

*

* @return response()

*/

public function updatedSelectedState($state)

{

if (!is_null($state)) {

$this->cities = City::where('state_id', $state)->get();

}

}

}

resources/views/livewire/statecitydropdown.blade.php

<div>

<h1>Laravel Livewire Dependant Dropdown - ItSolutionStuff.com</h1>

<div class="form-group row">

<label for="state" class="col-md-4 col-form-label text-md-right">State</label>

<div class="col-md-6">

<select wire:model="selectedState" class="form-control">

<option value="" selected>Choose state</option>

@foreach($states as $state)

<option value="{{ $state->id }}">{{ $state->name }}</option>

@endforeach

</select>

</div>

</div>

@if (!is_null($selectedState))

<div class="form-group row">

<label for="city" class="col-md-4 col-form-label text-md-right">City</label>

<div class="col-md-6">

<select class="form-control" name="city_id">

<option value="" selected>Choose city</option>

@foreach($cities as $city)

<option value="{{ $city->id }}">{{ $city->name }}</option>

@endforeach

</select>

</div>

</div>

@endif

</div>

Step 5: Create Route

now we will create one route for calling our example, so let's add new route to web.php file as bellow:

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Livewire\Statecitydropdown;

/*

|--------------------------------------------------------------------------

| 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('statecitydropdown', Statecitydropdown::class);

Step 6: Create View File

here, we will create blade file for call form route. in this file we will use @livewireStyles, @livewireScripts. so let's add it.

resources/views/layouts/app.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Livewire Example - ItSolutionStuff.com</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>

</head>

<body>

<div class="container">

@yield('content')

</div>

</body>

@livewireScripts

</html>

Now you can run using bellow command:

php artisan serve

Open bellow URL:

localhost:8000/statecitydropdown

I hope it can help you...

Tags :
Shares