Public API to access data of a resource with filters

Is there a way to access the raw data or query of a resource using the advanced filters plugin from the backend without instantiating the livewire components? The use case for this is: we are going to save some global filters sets (user views) and we want to store in each record to what user views belongs to after executed. I want to execute the table from a console command with a specific filter, and then update the db.
Solution:
Found a way in case someone is interested: ```php public function handle() {...
Jump to solution
1 Reply
Solution
Marcelo Andrade
Marcelo Andrade7mo ago
Found a way in case someone is interested:
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();
}
}
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();
}
}
Want results from more Discord servers?
Add your server