Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on relationship server side search and order while using accessor #5245

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/views/bread/browse.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
@endif
@foreach($dataType->browseRows as $row)
<th>
@if ($isServerSide && ($row->type !== 'relationship' || $row->details->type == 'belongsTo'))
@if ($isServerSide && in_array($row->field, $sortableColumns))
<a href="{{ $row->sortByUrl($orderBy, $sortOrder) }}">
@endif
{{ $row->getTranslatedAttribute('display_name') }}
Expand Down
38 changes: 35 additions & 3 deletions src/Http/Controllers/VoyagerBaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function index(Request $request)
$search_value = ($search->filter == 'equals') ? $search->value : '%'.$search->value.'%';

$searchField = $dataType->name.'.'.$search->key;
if ($row = $this->findRelationshipRow($dataType->rows->where('type', 'relationship'), $search->key)) {
if ($row = $this->findSearchableRelationshipRow($dataType->rows->where('type', 'relationship'), $search->key)) {
$query->whereIn(
$searchField,
$row->details->model::where($row->details->label, $search_filter, $search_value)->pluck('id')->toArray()
Expand Down Expand Up @@ -174,6 +174,9 @@ public function index(Request $request)
$orderColumn = [[$index, $sortOrder ?? 'desc']];
}

// Define list of columns that can be sorted server side
$sortableColumns = $this->getSortableColumns($dataType->browseRows);

$view = 'voyager::bread.browse';

if (view()->exists("voyager::$slug.browse")) {
Expand All @@ -188,6 +191,7 @@ public function index(Request $request)
'search',
'orderBy',
'orderColumn',
'sortableColumns',
'sortOrder',
'searchNames',
'isServerSide',
Expand Down Expand Up @@ -958,10 +962,38 @@ public function relation(Request $request)
return response()->json([], 404);
}

protected function findRelationshipRow($relationshipRows, $searchKey)
protected function findSearchableRelationshipRow($relationshipRows, $searchKey)
{
return $relationshipRows->filter(function ($item) use ($searchKey) {
return $item->details->type == 'belongsTo' && $item->details->column == $searchKey;
if ($item->details->column != $searchKey) {
return false;
}
if ($item->details->type != 'belongsTo') {
return false;
}

return !$this->relationIsUsingAccessorAsLabel($item->details);
})->first();
}

protected function getSortableColumns($rows)
{
return $rows->filter(function ($item) {
if ($item->type != 'relationship') {
return true;
}
if ($item->details->type != 'belongsTo') {
return false;
}

return !$this->relationIsUsingAccessorAsLabel($item->details);
})
->pluck('field')
->toArray();
}

protected function relationIsUsingAccessorAsLabel($details)
{
return in_array($details->label, app($details->model)->additional_attributes ?? []);
}
}