Fixed api issue

This commit is contained in:
m.razaghi
2026-07-12 13:16:57 +03:30
parent a95826e5e5
commit 737f06f68e

View File

@@ -7,6 +7,7 @@ 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;
@@ -94,17 +95,11 @@ class GlobalSearchApiController extends Controller
}); });
$results = $results->merge($opinions); $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']) $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}%");
->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}%"); });
}) })
->take($limitPerTable)->get()->map(function($item) use ($searchTerm) { ->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
$formatted = $this->formatArt($item, $searchTerm, Art::class); $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']) $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}%");
->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}%"); });
}); });
$totalCount += $artQuery->count(); $totalCount += $artQuery->count();
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) { $arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
@@ -407,7 +396,7 @@ class GlobalSearchApiController extends Controller
'price' => $law?->price, 'price' => $law?->price,
'image' => $law?->image, 'image' => $law?->image,
// Relational Objects // Relational Objects matching ArtController exactly
'law' => $item->law != null ? [ 'law' => $item->law != null ? [
'id' => $item->law->id, 'id' => $item->law->id,
'title' => $item->law->title, 'title' => $item->law->title,
@@ -474,6 +463,7 @@ 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,