public function handle()
{
$filterSets = UserView::whereIsGlobalFavorite(true)->get();
$user = User::findOrFail(1);
auth()->login($user);
$subjectsToUpdate = [];
/** @var UserView $filterSet */
foreach ($filterSets as $filterSet) {
echo $filterSet->name, PHP_EOL;
/** @var SubjectResource\Pages\ListSubjects $livewire */
$livewire = app(SubjectResource\Pages\ListSubjects::class);
$livewire->tableFilters = $filterSet->filters['tableFilters'];
$livewire->tableDeferredFilters = $filterSet->filters['tableFilters'];
$livewire->bootedInteractsWithTable();
$livewire->applyTableFilters();
/** @var Builder $query */
$query = $livewire->getFilteredSortedTableQuery();
$records = $livewire->getTableRecords();
foreach ($records as $subject) {
$subjectId = $subject->id;
if (array_key_exists($subjectId, $subjectsToUpdate)) {
$subjectsToUpdate[$subjectId] = array_merge($subjectsToUpdate[$subjectId], [$filterSet->id => $filterSet->name]);
} else {
$subjectsToUpdate[$subjectId] = [$filterSet->id => $filterSet->name];
}
}
}
foreach ($subjectsToUpdate as $subjectId => $filtersMatched) {
$subject = Subject::findOrFail($subjectId);
$subject->user_views = array_values($filtersMatched);
$subject->save();
}
}