Livewire通过复选框向company_user中间表赋值同步异常问题排查
修复方案
1. Livewire组件代码修改
修改updatedSelected方法,统一转换选中ID为整数,消除字符串/数字类型混用导致的同步异常:
public function updatedSelected() { $this->selected = array_map('intval', $this->selected); $this->user->companies()->sync($this->selected); }
2. Blade模板代码修改
修复复选框取消勾选不同步问题
给复选框的wire:model添加.array修饰符,这是Livewire绑定数组类型多选复选框的必填修饰符:
<input type="checkbox" value="{{ $company->id }}" wire:model.array="selected">
修复返回按钮跳转失效问题
<x-jet-button>默认输出<button>标签,嵌套在<a>标签内时默认行为会被Livewire拦截,修改为直接通过点击事件触发跳转即可:
<x-jet-button type="button" wire:click="window.location.href='{{ url()->previous() }}'">Back</x-jet-button>
内容的提问来源于stack exchange,提问作者zaster
相关产品推荐
相关产品推荐

