Fixed api issue #9
@@ -7,7 +7,6 @@ use App\Models\Art;
|
|||||||
use App\Models\Book;
|
use App\Models\Book;
|
||||||
use App\Models\Branch;
|
use App\Models\Branch;
|
||||||
use App\Models\BylawDocument;
|
use App\Models\BylawDocument;
|
||||||
use App\Models\BylawNode;
|
|
||||||
use App\Models\Chapter;
|
use App\Models\Chapter;
|
||||||
use App\Models\Division;
|
use App\Models\Division;
|
||||||
use App\Models\Gate;
|
use App\Models\Gate;
|
||||||
@@ -95,8 +94,8 @@ class GlobalSearchApiController extends Controller
|
|||||||
});
|
});
|
||||||
$results = $results->merge($opinions);
|
$results = $results->merge($opinions);
|
||||||
|
|
||||||
// 4. Search Arts (Optimized to mirror ArtController speed)
|
// 4. Search Arts (Exact match to ArtController logic + eager loading relations)
|
||||||
$arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
|
$arts = Art::with(['chapter', 'part', 'volum', 'law', '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}%");
|
||||||
@@ -250,7 +249,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'])
|
$artQuery = Art::with(['chapter', 'part', 'volum', 'law', '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}%");
|
||||||
@@ -369,12 +368,12 @@ class GlobalSearchApiController extends Controller
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function formatArt($item, $searchTerm, $modelClass)
|
private function formatArt($art, $searchTerm, $modelClass)
|
||||||
{
|
{
|
||||||
$law = $item->law ?? Law::find($item->law_id);
|
$law = $art->law ?? Law::find($art->law_id);
|
||||||
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
|
$isLocked = ! auth()->user()->isSubscriber() && (bool) $law?->is_locked;
|
||||||
|
|
||||||
$text = $item->text ?? '';
|
$text = $art->text ?? '';
|
||||||
$searchQuoted = preg_quote($searchTerm, '/');
|
$searchQuoted = preg_quote($searchTerm, '/');
|
||||||
if (!empty($text) && preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
|
if (!empty($text) && preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
|
||||||
$context = trim($matches[1] ?? '') . ' ' . $matches[2] . ' ' . trim($matches[3] ?? '');
|
$context = trim($matches[1] ?? '') . ' ' . $matches[2] . ' ' . trim($matches[3] ?? '');
|
||||||
@@ -382,55 +381,56 @@ class GlobalSearchApiController extends Controller
|
|||||||
$context = $text;
|
$context = $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PERFECTLY MATCHED TO YOUR ARTCONTROLLER MAPPING
|
||||||
return [
|
return [
|
||||||
'id' => $item->id,
|
'id' => $art->id,
|
||||||
'title' => $item->title,
|
'title' => $art->title,
|
||||||
'number' => $item->number,
|
'number' => $art->number,
|
||||||
'text' => $isLocked ? null : $context,
|
'text' => $isLocked ? null : $context,
|
||||||
'is_locked' => $isLocked,
|
'is_locked' => $isLocked,
|
||||||
'route' => array_values($this->route($modelClass, $item)),
|
'route' => array_values($this->route($modelClass, $art)),
|
||||||
'category' => optional($law->category)->name,
|
'category' => optional($law?->category)->name,
|
||||||
|
|
||||||
'count_art' => $law?->arts?->count() ?? 0,
|
'count_art' => $law?->arts?->count() ?? 0,
|
||||||
'count_volums'=> $law?->volums?->count() ?? 0,
|
'count_volums'=> $law?->volums?->count() ?? 0,
|
||||||
'price' => $law?->price,
|
'price' => $law?->price,
|
||||||
'image' => $law?->image,
|
'image' => $law?->image,
|
||||||
|
|
||||||
// Relational Objects matching ArtController exactly
|
// Full nested relationship mappings
|
||||||
'law' => $item->law != null ? [
|
'chapter' => $art->chapter != null ? [
|
||||||
'id' => $item->law->id,
|
'id' => $art->chapter->id,
|
||||||
'title' => $item->law->title,
|
'title' => $art->chapter->title,
|
||||||
'number' => $item->law->number,
|
'number' => $art->chapter->number,
|
||||||
] : null,
|
] : null,
|
||||||
'chapter' => $item->chapter != null ? [
|
'part' => $art->part != null ? [
|
||||||
'id' => $item->chapter->id,
|
'id' => $art->part->id,
|
||||||
'title' => $item->chapter->title,
|
'title' => $art->part->title,
|
||||||
'number' => $item->chapter->number,
|
'number' => $art->part->number,
|
||||||
] : null,
|
] : null,
|
||||||
'part' => $item->part != null ? [
|
'volum' => $art->volum != null ? [
|
||||||
'id' => $item->part->id,
|
'id' => $art->volum->id,
|
||||||
'title' => $item->part->title,
|
'title' => $art->volum->title,
|
||||||
'number' => $item->part->number,
|
'number' => $art->volum->number,
|
||||||
] : null,
|
] : null,
|
||||||
'volum' => $item->volum != null ? [
|
'law' => $art->law != null ? [
|
||||||
'id' => $item->volum->id,
|
'id' => $art->law->id,
|
||||||
'title' => $item->volum->title,
|
'title' => $art->law->title,
|
||||||
'number' => $item->volum->number,
|
'number' => $art->law->number,
|
||||||
] : null,
|
] : null,
|
||||||
'book' => $item->book != null ? [
|
'book' => $art->book != null ? [
|
||||||
'id' => $item->book->id,
|
'id' => $art->book->id,
|
||||||
'title' => $item->book->title,
|
'title' => $art->book->title,
|
||||||
'number' => $item->book->number,
|
'number' => $art->book->number,
|
||||||
] : null,
|
] : null,
|
||||||
'section' => $item->section != null ? [
|
'section' => $art->section != null ? [
|
||||||
'id' => $item->section->id,
|
'id' => $art->section->id,
|
||||||
'title' => $item->section->title,
|
'title' => $art->section->title,
|
||||||
'number' => $item->section->number,
|
'number' => $art->section->number,
|
||||||
] : null,
|
] : null,
|
||||||
'gate' => $item->gate != null ? [
|
'gate' => $art->gate != null ? [
|
||||||
'id' => $item->gate->id,
|
'id' => $art->gate->id,
|
||||||
'title' => $item->gate->title,
|
'title' => $art->gate->title,
|
||||||
'number' => $item->gate->number,
|
'number' => $art->gate->number,
|
||||||
] : null,
|
] : null,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -463,18 +463,17 @@ class GlobalSearchApiController extends Controller
|
|||||||
|
|
||||||
private function route($modelClass, $query)
|
private function route($modelClass, $query)
|
||||||
{
|
{
|
||||||
// Safe Breadcrumb Building - Uses relation if loaded, else manual lookup.
|
|
||||||
if ((new $modelClass)->getTable() == 'art') {
|
if ((new $modelClass)->getTable() == 'art') {
|
||||||
$route = array_filter([
|
$route = array_filter([
|
||||||
$query->relationLoaded('law') ? $query->law?->title : Law::find($query->law_id)?->title,
|
$query->relationLoaded('law') ? $query->law?->title : Law::find($query->law_id)?->title,
|
||||||
$query->relationLoaded('volum') ? $query->volum?->title : Volum::find($query->volum_id)?->title,
|
$query->relationLoaded('volum') ? $query->volum?->title : Volum::find($query->volum_id)?->title,
|
||||||
$query->relationLoaded('book') ? $query->book?->title : Book::find($query->book_id)?->title,
|
$query->relationLoaded('book') ? $query->book?->title : Book::find($query->book_id)?->title,
|
||||||
Division::find($query->division_id)?->title,
|
Division::find($query->division_id)?->title, // Division is not in eagerly loaded relations, query safely
|
||||||
$query->relationLoaded('section') ? $query->section?->title : Section::find($query->section_id)?->title,
|
$query->relationLoaded('section') ? $query->section?->title : Section::find($query->section_id)?->title,
|
||||||
$query->relationLoaded('chapter') ? $query->chapter?->title : Chapter::find($query->chapter_id)?->title,
|
$query->relationLoaded('chapter') ? $query->chapter?->title : Chapter::find($query->chapter_id)?->title,
|
||||||
$query->relationLoaded('part') ? $query->part?->title : Part::find($query->part_id)?->title,
|
$query->relationLoaded('part') ? $query->part?->title : Part::find($query->part_id)?->title,
|
||||||
$query->relationLoaded('gate') ? $query->gate?->title : Gate::find($query->gate_id)?->title,
|
$query->relationLoaded('gate') ? $query->gate?->title : Gate::find($query->gate_id)?->title,
|
||||||
Branch::find($query->branch_id)?->title,
|
Branch::find($query->branch_id)?->title, // Branch is not in eagerly loaded relations, query safely
|
||||||
]);
|
]);
|
||||||
return array_values($route);
|
return array_values($route);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user