3 回答
TA贡献1834条经验 获得超8个赞
您的实现的问题是,在选择框中您在刀片中回显的是string,而不是object,因此线模式将通过获取输入事件的值并将其与后端同步来完成其工作。有两种方法可以实现您的目标。
将 select 选项的值作为制造商 id,然后您可以直接在更新的生命周期挂钩中使用它。
<label for="manufacturer_id">Manufacturer</label>
<select wire:model="manufacturer_id" id="manufacturer_id">
<option selected="selected" disabled>Select a Manufacturer</option>
@foreach($manufacturers as $selectableManufacturer)
<option value="{{ $selectableManufacturer->id }}">{{ $selectableManufacturer->name }}</option>
@endforeach
</select>
在更新的钩子中,
public $manufacturer_id;
public function updated()
{
$this->cars = Car::where('manufacturer_id', $this->manufacturer_id)->get();
}
或者只是使用该json_decode()函数使其成为一个对象,然后像您所做的那样访问 id 属性。
public function updated()
{
$manufacturer = json_decode($this->manufacturer);
$this->cars = Car::where('manufacturer_id', $manufacturer->id)->get();
}
但健康的方法是使用 id 作为值。
TA贡献1845条经验 获得超8个赞
您应该同步id所选制造商的 并在更新的挂钩中使用它。以下是您可以如何做到这一点。
class PostComponent extends Component
{
public $manufacturers;
public $manufacturer_id;
public $manufacturer = 'Select a Manufacturer';
public $cars;
public function mount()
{
$this->manufacturers = Manufacturer::orderBy('name')->get();
}
public function updatedManufacturerId()
{
$this->cars = Car::where('manufacturer_id', $this->manufacturer_id)->get();
}
public function render()
{
return view('livewire.post-component');
}
}
前端可以保持不变。
<label for="manufacturer_id">Manufacturer</label>
<select wire:model="manufacturer_id" id="manufacturer_id">
<option selected="selected" disabled>Select a Manufacturer</option>
@foreach($manufacturers as $selectableManufacturer)
<option value="{{ $selectableManufacturer->id }}">{{ $selectableManufacturer->name }}</option>
@endforeach
</select>
TA贡献1877条经验 获得超6个赞
- 3 回答
- 0 关注
- 134 浏览
添加回答
举报