Created
December 7, 2023 20:40
-
-
Save yelocode/8a7e7a8ec5d5e834eb6b4f95a5ba227f to your computer and use it in GitHub Desktop.
Livewire 3 dependent Dropdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
return new class extends Migration | |
{ | |
/** | |
* Run the migrations. | |
*/ | |
public function up(): void | |
{ | |
Schema::create('companies', function (Blueprint $table) { | |
$table->id(); | |
$table->string('name'); | |
$table->string('description'); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
*/ | |
public function down(): void | |
{ | |
Schema::dropIfExists('companies'); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<br> | |
<select wire:model.live="companyID" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg"> | |
<option value="">Select Company</option> | |
@foreach ($this->companies as $company) | |
<option value="{{ $company->id }}">{{ $company->name }}</option> | |
@endforeach | |
</select> | |
<br> | |
<select wire:model.live="phoneID" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg"> | |
<option value="">Select phone</option> | |
@foreach ($this->phones as $phone) | |
<option value="{{ $phone->id }}">{{ $phone->name }}</option> | |
@endforeach | |
</select> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Livewire; | |
use App\Models\Company; | |
use App\Models\Phone; | |
use Livewire\Attributes\Computed; | |
use Livewire\Component; | |
class Form extends Component | |
{ | |
public $companyID; | |
public $phoneID; | |
public function updatedCompanyID() | |
{ | |
$this->phoneID = null; | |
} | |
#[Computed()] | |
public function companies() | |
{ | |
return Company::all(); | |
} | |
#[Computed()] | |
public function phones() | |
{ | |
return Phone::where('company_id', $this->companyID)->get(); | |
} | |
public function render() | |
{ | |
return view( | |
'livewire.form' | |
); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Models\Company; | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Support\Facades\Schema; | |
return new class extends Migration | |
{ | |
/** | |
* Run the migrations. | |
*/ | |
public function up(): void | |
{ | |
Schema::create('phones', function (Blueprint $table) { | |
$table->id(); | |
$table->foreignIdFor(Company::class)->index(); // company_id | |
$table->string('name'); | |
$table->string('description'); | |
$table->timestamps(); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
*/ | |
public function down(): void | |
{ | |
Schema::dropIfExists('phones'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment