Laravel Livewire Dependant Dropdown Example

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 and laravel 8 version.

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

Laravel Livewire Dependant Dropdown Example

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 IlluminateDatabaseMigrationsMigration;

use IlluminateDatabaseSchemaBlueprint;

use IlluminateSupportFacadesSchema;

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 AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;

use IlluminateDatabaseEloquentModel;

class State extends Model

{

use HasFactory;

protected $fillable = ['name'];

}

app/Models/City.php

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;

use IlluminateDatabaseEloquentModel;

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 AppHttpLivewire;

use LivewireComponent;

use AppModelsCity;

use AppModelsState;

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 IlluminateSupportFacadesRoute;

use AppHttpLivewireStatecitydropdown;

/*

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

| 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…

Leave a Comment