Skip to content

Commit

Permalink
Update VoyagerBaseController.php (#5916)
Browse files Browse the repository at this point in the history
Enable sort for relationship full data, not only page per page dataset

Co-authored-by: toto975 <[email protected]>
  • Loading branch information
bobbyiliev and toto975 authored Oct 14, 2024
1 parent 0623095 commit 1abc1cf
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Http/Controllers/VoyagerBaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -917,23 +917,36 @@ public function relation(Request $request)
if ($search) {
// If we are using additional_attribute as label
if (in_array($options->label, $additional_attributes)) {
$relationshipOptions = $model->get();
$relationshipOptions = $model;
$relationshipOptions = $relationshipOptions->filter(function ($model) use ($search, $options) {
return stripos($model->{$options->label}, $search) !== false;
});
$total_count = $relationshipOptions->count();
$relationshipOptions = $relationshipOptions->forPage($page, $on_page);
} else {
$total_count = $model->where($options->label, 'LIKE', '%'.$search.'%')->count();
$relationshipOptions = $model->take($on_page)->skip($skip)
->where($options->label, 'LIKE', '%'.$search.'%')
->get();
$total_count = $model->where($options->label, 'LIKE', '%' . $search . '%')->count();
$relationshipOptions = $model->where($options->label, 'LIKE', '%' . $search . '%');
}
} else {
$total_count = $model->count();
$relationshipOptions = $model->take($on_page)->skip($skip)->get();
$relationshipOptions = $model;
}

// Sort results
if (!empty($options->sort->field)) {
$sort = SORT_REGULAR;
if (!empty($options->sort->flag)) {
$sort = str_replace('"', '', $options->sort->flag);
}
if (!empty($options->sort->direction)) {
$relationshipOptions = $relationshipOptions->orderBy($options->sort->field, $options->sort->direction);
}
}

$relationshipOptions = $relationshipOptions->get()
->skip($skip)
->take($on_page);


$results = [];

if (!$row->required && !$search && $page == 1) {
Expand Down

0 comments on commit 1abc1cf

Please sign in to comment.