就我而言,问题来自数据库查询和结果集合。
这个查询(变量的含义不重要)
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,
},
],
该查询由刀片文件中的按钮触发。单击按钮 2 或 3 次导致水合错误。
在 Livewire 文档中解释了数组键有时会导致问题:https ://laravel-livewire.com/docs/2.x/troubleshooting
所以我试图从 db 查询结果中创建一个普通数组。因为我只需要钥匙,所以这很有效:
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(); // ADDED THIS
现在结果是:
[
454,
528,
531,
]
这解决了水合错误。