diff --git a/app/Http/Controllers/api/GlobalSearchApiController.php b/app/Http/Controllers/api/GlobalSearchApiController.php index 405b40c..df101cd 100644 --- a/app/Http/Controllers/api/GlobalSearchApiController.php +++ b/app/Http/Controllers/api/GlobalSearchApiController.php @@ -7,6 +7,7 @@ use App\Models\Art; use App\Models\Book; use App\Models\Branch; use App\Models\BylawDocument; +use App\Models\BylawNode; use App\Models\Chapter; use App\Models\Division; use App\Models\Gate; @@ -94,17 +95,11 @@ class GlobalSearchApiController extends Controller }); $results = $results->merge($opinions); - // 4. Search Arts + // 4. Search Arts (Optimized to mirror ArtController speed) $arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate']) ->where(function ($q) use ($searchTerm) { $q->where('title', 'LIKE', "%{$searchTerm}%") - ->orWhere('text', 'LIKE', "%{$searchTerm}%") - ->orWhereHas('law', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) - ->orWhereHas('volum', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) - ->orWhereHas('book', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) - ->orWhereHas('section', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) - ->orWhereHas('chapter', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) - ->orWhereHas('part', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }); + ->orWhere('text', 'LIKE', "%{$searchTerm}%"); }) ->take($limitPerTable)->get()->map(function($item) use ($searchTerm) { $formatted = $this->formatArt($item, $searchTerm, Art::class); @@ -258,13 +253,7 @@ class GlobalSearchApiController extends Controller $artQuery = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate']) ->where(function ($q) use ($searchTerm) { $q->where('title', 'LIKE', "%{$searchTerm}%") - ->orWhere('text', 'LIKE', "%{$searchTerm}%") - ->orWhereHas('law', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) - ->orWhereHas('volum', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) - ->orWhereHas('book', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) - ->orWhereHas('section', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) - ->orWhereHas('chapter', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }) - ->orWhereHas('part', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); }); + ->orWhere('text', 'LIKE', "%{$searchTerm}%"); }); $totalCount += $artQuery->count(); $arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) { @@ -407,7 +396,7 @@ class GlobalSearchApiController extends Controller 'price' => $law?->price, 'image' => $law?->image, - // Relational Objects + // Relational Objects matching ArtController exactly 'law' => $item->law != null ? [ 'id' => $item->law->id, 'title' => $item->law->title, @@ -474,6 +463,7 @@ class GlobalSearchApiController extends Controller private function route($modelClass, $query) { + // Safe Breadcrumb Building - Uses relation if loaded, else manual lookup. if ((new $modelClass)->getTable() == 'art') { $route = array_filter([ $query->relationLoaded('law') ? $query->law?->title : Law::find($query->law_id)?->title,