Fixed api issue #7

Merged
soheilkhaledabadi merged 2 commits from new-api into clean-main 2026-07-12 09:13:48 +00:00
Showing only changes of commit 398fcb4eec - Show all commits

View File

@@ -94,8 +94,8 @@ class GlobalSearchApiController extends Controller
}); });
$results = $results->merge($opinions); $results = $results->merge($opinions);
// 4. Search Arts (Deep Relational Search & Eager Loading) // 4. Search Arts (Removed missing 'division' and 'branch' from eager load)
$arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate', 'division', 'branch']) $arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
->where(function ($q) use ($searchTerm) { ->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%") $q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%") ->orWhere('text', 'LIKE', "%{$searchTerm}%")
@@ -255,7 +255,7 @@ class GlobalSearchApiController extends Controller
$results = $results->merge($opinions); $results = $results->merge($opinions);
// 4. Search Arts // 4. Search Arts
$artQuery = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate', 'division', 'branch']) $artQuery = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
->where(function ($q) use ($searchTerm) { ->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%") $q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%") ->orWhere('text', 'LIKE', "%{$searchTerm}%")
@@ -446,7 +446,6 @@ class GlobalSearchApiController extends Controller
'number' => $item->number, 'number' => $item->number,
'is_locked' => $isLocked, 'is_locked' => $isLocked,
'route' => array_values($this->route($modelClass, $item)), 'route' => array_values($this->route($modelClass, $item)),
// Keep law payload explicitly as an object to prevent DTO parsing errors
'law' => $law ? [ 'law' => $law ? [
'id' => $law->id, 'id' => $law->id,
'title' => $law->title, 'title' => $law->title,
@@ -464,18 +463,19 @@ class GlobalSearchApiController extends Controller
private function route($modelClass, $query) private function route($modelClass, $query)
{ {
// Now safely uses eager loaded relations if they exist! // Safe Breadcrumb Building - Uses relation if loaded, else manual lookup.
// Explicitly forces manual lookup for division and branch to avoid crash.
if ((new $modelClass)->getTable() == 'art') { if ((new $modelClass)->getTable() == 'art') {
$route = array_filter([ $route = array_filter([
$query->law->title ?? Law::find($query->law_id)?->title, $query->relationLoaded('law') ? $query->law?->title : Law::find($query->law_id)?->title,
$query->volum->title ?? Volum::find($query->volum_id)?->title, $query->relationLoaded('volum') ? $query->volum?->title : Volum::find($query->volum_id)?->title,
$query->book->title ?? Book::find($query->book_id)?->title, $query->relationLoaded('book') ? $query->book?->title : Book::find($query->book_id)?->title,
$query->division->title ?? Division::find($query->division_id)?->title, Division::find($query->division_id)?->title,
$query->section->title ?? Section::find($query->section_id)?->title, $query->relationLoaded('section') ? $query->section?->title : Section::find($query->section_id)?->title,
$query->chapter->title ?? Chapter::find($query->chapter_id)?->title, $query->relationLoaded('chapter') ? $query->chapter?->title : Chapter::find($query->chapter_id)?->title,
$query->part->title ?? Part::find($query->part_id)?->title, $query->relationLoaded('part') ? $query->part?->title : Part::find($query->part_id)?->title,
$query->gate->title ?? Gate::find($query->gate_id)?->title, $query->relationLoaded('gate') ? $query->gate?->title : Gate::find($query->gate_id)?->title,
$query->branch->title ?? Branch::find($query->branch_id)?->title, Branch::find($query->branch_id)?->title,
]); ]);
return array_values($route); return array_values($route);
} }