I would use a dynamic component and change the variable for which one is used
Feb 6, 2022
18
Level 54
Livewire Subview Dependent Upon Dropdown
I have a form with 6 different inputs on it, 1 of which is a select dropdown. What I am wanting to do is once the dropdown is changed it will inject a partial into the form based on the user's selection. Currently my page initially loads fine however I"m trying to have it render the child view into this form based on the user's input. Something I need to also figure out is passing the value of the selected option from the select into the changeCustomView method so I can know which child view to render.
<x-form.inputs.select
...
id="someId"
wire:click="changeCustomView"
/>
@if ($viewToRender)
{{ $viewToRender }}
@endif
<?php
namespace App\Http\Livewire\Foo;
use App\Http\Livewire\BaseComponent;
use Illuminate\Support\Facades\View;
use App\Models\MyClass;
class Form extends BaseComponent
{
public $myVar;
public $viewToRender = '';
public function mount(MyClass $myVar)
{
$this->myVar = $myVar;
}
public function changeCustomView()
{
return $this->viewToRender = View::make('livewire.views.types.foobar')->render();
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\View\View
*/
public function render()
{
return view('livewire.foo.form', [
'myVar' => $this->myVar,
]);
}
}
Level 51
working example, tested. blade files named test and test2 in the components directory...
<select wire:model="valuee">
<option>test</option>
<option>test2</option>
</select>
<x-dynamic-component :component="$componentName" class="mt-4" />
public $componentName = 'test';
public $valuee;
public function updatedValuee()
{
$this->componentName = $this->valuee;
}
1 like
Please or to participate in this conversation.