【问题标题】:Laravel/Livewire dynamic dropdown not sending the selected valueLaravel/Livewire 动态下拉列表不发送所选值
【发布时间】:2022-01-16 07:58:35
【问题描述】:

使用 Laravel 8 + Livewire: 我正在关注创建动态下拉列表的教程:https://www.itsolutionstuff.com/post/laravel-livewire-dependant-dropdown-exampleexample.html

我有这个下拉组件:

public $suppliers;
    public $beans;
    public $selectedSupplier = NULL;

    public function mount()
    {
        $this->suppliers = Supplier::whereHas('greenBeans')->get();
        
        $this->beans = collect();
    }

    public function render()
    {
        return view('livewire.admin.supplier-beans-dropdown')->layout('layouts.admin.livewire');
    }  

public function updatedSelectedSupplier($supplier)
{
    if (!is_null($supplier)) {
        $this->selectedSupplier = Supplier::find($supplier);
        $this->beans = $this->selectedSupplier->greenBeans;
    }
}

组件的刀片:

<div>
<div class="form-group">
    <label for="supplier" class="col-md-4 form-label">Supplier</label>
    <select wire:model.lazy="selectedSupplier" class="form-control">
        <option value="" selected>Select Supplier</option>
        @foreach($suppliers as $supplier)
            <option value="{{ $supplier->id }}">{{ $supplier->name }}</option>
        @endforeach
    </select>        
</div>

@if (!is_null($selectedSupplier))
    <div class="form-group">
        <label for="bean" class="form-label">Green Bean</label>
        <select class="form-control" name="bean" wire:model.lazy="selectedBean">
            <option value="" selected>Select Green Bean...</option>
                @foreach($beans as $bean)
                    <option value="{{ $bean->id }}">{{ $bean->name }}</option>
                @endforeach
        </select>
    </div>
@endif

我还有一个需要调用此下拉列表的组件。所以在这个父组件中我有这个 var: $selectedSupplier 并且在视图中我调用了下拉组件:

@livewire('admin.supplier-beans-dropdown')

当我选择供应商并提交表单时,selectedSupplier 为空。

那么如何在不同的组件中使用这个下拉菜单呢? 如何将选中的值发送给父组件?

【问题讨论】:

  • 如果相关问题,您可以发布组件的刀片或至少是html部分代码吗?

标签: laravel-8 laravel-livewire cascadingdropdown


【解决方案1】:

首先检查一下我认为你这里有一些错误。你有:

public function updatedSelectedSupplier($supplier)
{
  if (!is_null($supplier)) {
    $this->supplier = Supplier::find(supplier); --> typo error here???
    $this->beans = $supplier->greenBeans; --> $supplier parameter is an ID???
  }
} 

我认为应该是:

if (!is_null($supplier)) {
    $this->supplier = Supplier::find($supplier); 
    $this->beans = $this->supplier->greenBeans;
}

【讨论】:

  • 嗨,谢谢。我实际上看到了这个问题并修复了它,但忘记在这里更新它。我用修复和组件的刀片编辑了我的帖子。
猜你喜欢
  • 1970-01-01
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 2015-06-16
  • 2021-05-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多