在我的情况下,问题来自于数据库查询和结果集合。这个查询(变量的含义并不重要)
DB::table('progress_trackings')
->select('user_id')
->whereIn('user_id', $users->pluck('id'))
->where('object_id', $activity->id)
->where('event', $type)
->get()
->keyBy('user_id');
生成了这个输出:
Illuminate\Support\Collection {#2427
all: [
454 => {#2279
+"user_id": 454,
},
528 => {#441
+"user_id": 528,
},
531 => {#2368
+"user_id": 531,
},
],
查询是由 Blade 文件中的按钮触发的。
点击 2 或 3 次按钮会导致 hydration 错误。
在 Livewire 文档中解释了数组键有时可能会引起问题:https://laravel-livewire.com/docs/2.x/troubleshooting
因此,我尝试将数据库查询结果转换为简单的数组。由于我只需要键,所以这个方法可行:
DB::table('progress_trackings')
->select('user_id')
->whereIn('user_id', $users->pluck('id'))
->where('object_id', $activity->id)
->where('event', $type)
->get()
->keyBy('user_id')
->keys()->toArray();
现在的结果是:
[
454,
528,
531,
]
解决了hydration错误。
$protectedProperty
,那么你也可以将该属性传递给你的方法,像这样:wire:click="add(@js($protectedProperty))"
。 - Lenor