Laravel Livewire 更新时未将数据保存在数组中
Laravel Livewire not persisting data in array when updating
我有一个叫 public $items = []
的 属性。这可以有多个嵌套数组,即:$items.1, $items.2, $items.3
等等。其中每个子数组至少需要保存两条数据。
$items[1]['prop1'] = "value 1";
$items[1]['prop2'] = "value 2";
$items[2]['prop1'] = "value 1";
$items[2]['prop2'] = "value 2";
我的问题是,在我看来,我有两个输入绑定到这些子属性。
然后我根据这个输入添加一个新的数组条目。因此,如果用户在 prop1 中输入了某个东西,数组将包含一个基于此输入的自动生成的元素。
问题来了。当我更改 prop1
并在 updatedItems
生命周期挂钩中捕获它时,它是正确的。但是当我更改 prop2
时,它会覆盖 prop1
并且数组只包含一个值。
public function updatedItems($value, $nested)
{
$nestedData = explode(".", $nested);
// Get the nested property that was changed. this will yield a string formated as "1.prop1" or "2.prop1"
// Let's pretend that the $value variable says "value 1" and the $nested variable is "1.prop1"
if ($nestedData[1] == 'prop1') {
$this->items[$nestedData[0]]["secondary_value"] = 'secondary value';
// This should yield an items array that looks like this: items[1]['secondary_value'] = 'secondary value'
}
if ($nestedData[1] == 'prop2') {
$this->items[$nestedData[0]]["third_value"] = 'third value';
// This should yield an items array that looks like this: items[1]['third_value'] = 'secondary value'
}
}
我的期望是我会得到一个 items
数组,如下所示:
$items = [
"1" =['prop1' => 'value 1', 'secondary_value' => 'secondary value'],
"2" =['prop2' => 'value 1', 'third_value' => 'third value']
]
但无论我做什么,items
数组仅包含一个值,即最近更改的值。因此,如果我先更改 prop1,然后再更改 prop2,则数组将仅包含 prop2,并且不会附加到数组,但基本上会重新创建仅包含该值的整个数组。
我查看了其他多个问题,建议添加我已定义的 $rules
属性,但仍然无法解决问题。我什至尝试设置一个会话变量,但似乎有同样的问题,每次都被覆盖。 livewire 是否使用某种与 laravels 原生会话系统冲突的会话系统?非常感谢任何有关使此数据持久化以便我的数组可以同时保存 input1 和 input2 的值的帮助。
查看
<tr>
<td colspan="5">
<select wire:model="items.1.right_eye_brand">
<option value=""></option>
@foreach ($sortedItems as $o)
<option value="{{ $o }}">{{ $o->brand }}</option>
@endforeach
</select>
</td>
</tr>
<tr>
<td colspan="5">
<select wire:model="items.1.left_eye_brand">
<option value=""></option>
@foreach ($sortedItems as $o)
<option value="{{ $o }}">{{ $o->brand }}</option>
@endforeach
</select>
</td>
</tr>
问题似乎与 Livewire 生命周期有关。
问题
所以我能够使用 laravelplayground.com 重现该问题。这里:
简答
不要 dump/dd 在 updatedItems
里面。
长答案
其实items
倾倒物品updatedItems
是不正确的。但是,如果您在 render
方法中转储,items
应该是正确的。
我将深入搜索以了解发生这种情况的原因。但我猜想与原始 items
和 updatedItems 方法中使用的 items
有某种合并,并且合并发生在 update*
方法 运行 和 [=14= 之前] 被调用。
我在 laravel 游乐场做了另一个例子,它在视图组件中显示了项目内容,正如你所看到的,返回的数据总是正确的。
我有一个叫 public $items = []
的 属性。这可以有多个嵌套数组,即:$items.1, $items.2, $items.3
等等。其中每个子数组至少需要保存两条数据。
$items[1]['prop1'] = "value 1";
$items[1]['prop2'] = "value 2";
$items[2]['prop1'] = "value 1";
$items[2]['prop2'] = "value 2";
我的问题是,在我看来,我有两个输入绑定到这些子属性。
然后我根据这个输入添加一个新的数组条目。因此,如果用户在 prop1 中输入了某个东西,数组将包含一个基于此输入的自动生成的元素。
问题来了。当我更改 prop1
并在 updatedItems
生命周期挂钩中捕获它时,它是正确的。但是当我更改 prop2
时,它会覆盖 prop1
并且数组只包含一个值。
public function updatedItems($value, $nested)
{
$nestedData = explode(".", $nested);
// Get the nested property that was changed. this will yield a string formated as "1.prop1" or "2.prop1"
// Let's pretend that the $value variable says "value 1" and the $nested variable is "1.prop1"
if ($nestedData[1] == 'prop1') {
$this->items[$nestedData[0]]["secondary_value"] = 'secondary value';
// This should yield an items array that looks like this: items[1]['secondary_value'] = 'secondary value'
}
if ($nestedData[1] == 'prop2') {
$this->items[$nestedData[0]]["third_value"] = 'third value';
// This should yield an items array that looks like this: items[1]['third_value'] = 'secondary value'
}
}
我的期望是我会得到一个 items
数组,如下所示:
$items = [
"1" =['prop1' => 'value 1', 'secondary_value' => 'secondary value'],
"2" =['prop2' => 'value 1', 'third_value' => 'third value']
]
但无论我做什么,items
数组仅包含一个值,即最近更改的值。因此,如果我先更改 prop1,然后再更改 prop2,则数组将仅包含 prop2,并且不会附加到数组,但基本上会重新创建仅包含该值的整个数组。
我查看了其他多个问题,建议添加我已定义的 $rules
属性,但仍然无法解决问题。我什至尝试设置一个会话变量,但似乎有同样的问题,每次都被覆盖。 livewire 是否使用某种与 laravels 原生会话系统冲突的会话系统?非常感谢任何有关使此数据持久化以便我的数组可以同时保存 input1 和 input2 的值的帮助。
查看
<tr>
<td colspan="5">
<select wire:model="items.1.right_eye_brand">
<option value=""></option>
@foreach ($sortedItems as $o)
<option value="{{ $o }}">{{ $o->brand }}</option>
@endforeach
</select>
</td>
</tr>
<tr>
<td colspan="5">
<select wire:model="items.1.left_eye_brand">
<option value=""></option>
@foreach ($sortedItems as $o)
<option value="{{ $o }}">{{ $o->brand }}</option>
@endforeach
</select>
</td>
</tr>
问题似乎与 Livewire 生命周期有关。
问题
所以我能够使用 laravelplayground.com 重现该问题。这里:
简答
不要 dump/dd 在 updatedItems
里面。
长答案
其实items
倾倒物品updatedItems
是不正确的。但是,如果您在 render
方法中转储,items
应该是正确的。
我将深入搜索以了解发生这种情况的原因。但我猜想与原始 items
和 updatedItems 方法中使用的 items
有某种合并,并且合并发生在 update*
方法 运行 和 [=14= 之前] 被调用。
我在 laravel 游乐场做了另一个例子,它在视图组件中显示了项目内容,正如你所看到的,返回的数据总是正确的。