You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Livewire选择分支后门店列表无法更新的问题排查

选择分支后门店下拉列表不自动更新的问题

我需要实现选择分支(一个分支对应多个门店)时自动更新门店下拉列表,但操作后无任何效果。先后尝试两种方案均无效,甚至用dd()调试都没反应:

  • 在组件的updated()方法中监听branch_id属性变化,查询对应门店并赋值给stores变量;
  • 使用专门的updatedBranchId()方法查询门店。

Blade视图代码

<div class="row">
    <div class="col-lg-6">
        <div class="mb-3">
            <label class="form-label">Transaction Type</label> <br />
            <select class="form-control" wire:model="type">
                <option>--Select Type--</option>
                @foreach (['Buying'] as $type)
                    <option value="{{ strtolower($type) }}">{{ $type }}
                    </option>
                @endforeach
            </select>
            @error('type')
                <span class="text-danger">{{ $message }}</span>
            @enderror
        </div>
    </div>
    <div class="col-lg-6">
        <div class="mb-3">
            <label class="form-label">Branch</label> <br />
            <select class="form-control" wire:model.live="branch_id">
                <option>--Select branch--</option>
                @forelse ($branches as $branch)
                    <option value="{{ $branch->id }}">{{ $branch->name }}</option>
                @empty
                    <option value="">No branches were found</option>
                @endforelse
            </select>
            @error('branch_id')
                <span class="text-danger">{{ $message }}</span>
            @enderror
        </div>
    </div>
</div>
<div class="row">
    <div class="col-lg-6">
        <div class="mb-3">
            <label class="form-label">Store</label> <br />
            <select class="form-control" wire:model.live="store_id">
                <option>--Select store--</option>
                @forelse ($stores as $store)
                    <option value="{{ $store->id }}">
                        {{ $store->name }}
                    </option>
                @empty
                    <option value="">No stores were found</option>
                @endforelse
            </select>
            @error('store_id')
                <span class="text-danger">{{ $message }}</span>
            @enderror
        </div>
    </div>
</div>

PHP组件方法代码

public function updated($property)
{
    if ($property == 'branch_id') {
        $this->stores = Store::query()
            ->where('branch_id', $this->branch_id ?? -1) // 若未设置分支ID,则返回空
            ->orderBy('name')
            ->get();
    }

    if (preg_match("/^fields\.(\d+)\.[a-zA-Z]+$/", $property, $matches)) {
        $index = $matches[1];
        $this->fields[$index]['amount_in_forex'] = empty($this->fields[$index]['amount_in_forex'])
            ? 0
            : intval($this->fields[$index]['amount_in_forex']);
        $this->fields[$index]['exchange_rate'] = empty($this->fields[$index]['exchange_rate'])
            ? 0
            : intval($this->fields[$index]['exchange_rate']);
        $this->fields[$index]['sub_total'] = $this->fields[$index]['exchange_rate'] * $this->fields[$index]['amount_in_forex'];
        $this->calculate_totals();
    }

    if ($property == 'customer_id') {
        $this->customerIds = CustomerID::where('customer_id', $this->customer_id)->get();
    }
}

尝试过的无效代码

Blade部分

<div class="col-lg-6">
    <div class="mb-3">
        <label class="form-label">Branch</label> <br />
        <select class="form-control" wire:model.live="branch_id">
            <option>--Select branch--</option>
            @forelse ($this->branches as $branch)
                <option value="{{ $branch->id }}">{{ $branch->name }}</option>
            @empty
                <option value="">No branches were found</option>
            @endforelse
        </select>
        @error('branch_id')
            <span class="text-danger">{{ $message }}</span>
        @enderror
    </div>
</div>

PHP方法部分

public function updatedBranchId(){
    return $stores = Store::query()
        ->where('branch_id', $this->branch_id ?? -1) // 若未设置分支ID,则返回空
        ->orderBy('name')
        ->get();
}

内容的提问来源于stack exchange,提问作者davidkihara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 15:43:14