Skip to content

Commit

Permalink
Prevent empty parents from being found
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofandel committed Jul 11, 2024
1 parent 0473dbc commit 67efcf5
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/Http/Controllers/Admin/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ public function search(Request $request): Collection
})->map(function ($module) use ($request) {
$repository = $this->getRepository($module['name'], $module['repository'] ?? null);

$found = $repository->cmsSearch($request->get('search'), $module['search_fields'] ?? ['title'])->take(10);
$found = $repository->cmsSearch(
$request->get('search'),
$module['search_fields'] ?? ['title'],
isset($module['parentRelationship']) ? fn ($q) => $q->whereHas($module['parentRelationship']) : null
)->take(10);

return $found->map(function ($item) use ($module) {
try {
Expand All @@ -141,14 +145,8 @@ public function search(Request $request): Collection
/** @var BelongsTo $parent */
$parent = call_user_func([$item, $module['parentRelationship']]);
$parent_id = $parent->getParentKey();

if (empty($parent_id)) {
// Prevent module route error
return null;
}
}


return [
'id' => $item->id,
'href' => moduleRoute(
Expand Down Expand Up @@ -572,7 +570,7 @@ private function getDrafts(Collection $modules): Collection
if ($repository->hasBehavior('revisions')) {
$query->mine();
}

if (isset($module['parentRelationship'])) {
/** @var BelongsTo $parent */
$query->whereHas($module['parentRelationship']);
Expand Down

0 comments on commit 67efcf5

Please sign in to comment.