Fixed api issue

This commit is contained in:
m.razaghi
2026-07-12 16:02:53 +03:30
parent 10463e2f71
commit e4dad6f9a4

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;
@@ -26,7 +27,7 @@ class GlobalSearchApiController extends Controller
use BaseApiResponse; use BaseApiResponse;
/** /**
* Massive Search across ALL data types * Massive Search across ALL data types (With dynamic Date/Number Sorting & Deep Art Search)
*/ */
public function search(Request $request) public function search(Request $request)
{ {
@@ -45,7 +46,7 @@ class GlobalSearchApiController extends Controller
$results = collect(); $results = collect();
if (!empty($searchTerm)) { if (!empty($searchTerm)) {
$limitPerTable = 100; $limitPerTable = 100; // Performance safeguard
// 1. Search Bylaw Documents // 1. Search Bylaw Documents
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) $bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
@@ -65,7 +66,7 @@ class GlobalSearchApiController extends Controller
$formatted['type'] = 'bylaw'; $formatted['type'] = 'bylaw';
return $formatted; return $formatted;
}); });
$results = $results->merge($bylaws); $results = $results->concat($bylaws); // Changed to concat to prevent overwriting
// 2. Search Judicial Precedents // 2. Search Judicial Precedents
$judicials = JudicialPrecedent::with('category') $judicials = JudicialPrecedent::with('category')
@@ -78,7 +79,7 @@ class GlobalSearchApiController extends Controller
$formatted['type'] = 'judicial_precedent'; $formatted['type'] = 'judicial_precedent';
return $formatted; return $formatted;
}); });
$results = $results->merge($judicials); $results = $results->concat($judicials);
// 3. Search Legal Opinions // 3. Search Legal Opinions
$opinions = LegalOpinion::with('category') $opinions = LegalOpinion::with('category')
@@ -92,21 +93,28 @@ class GlobalSearchApiController extends Controller
$formatted['type'] = 'legal_opinion'; $formatted['type'] = 'legal_opinion';
return $formatted; return $formatted;
}); });
$results = $results->merge($opinions); $results = $results->concat($opinions);
// 4. Search Arts (Exact fast Fash search Fash search Fash search Fash search match Fash search Fash search Fash search to ArtController) // 4. Search Arts (Fully restored deep relational search without breaking relationships)
$arts = Art::with(['law', 'law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate']) $arts = Art::with(['law', '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}%")
->orWhere('number', 'LIKE', "%{$searchTerm}%"); ->orWhere('number', '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}%"); })
->orWhereHas('gate', 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);
$formatted['type'] = 'art'; $formatted['type'] = 'art';
return $formatted; return $formatted;
}); });
$results = $results->merge($arts); $results = $results->concat($arts);
// 5. Search Laws and Structural Models // 5. Search Laws and Structural Models
$structuralModels = [ $structuralModels = [
@@ -128,7 +136,7 @@ class GlobalSearchApiController extends Controller
return $formatted; return $formatted;
}); });
$results = $results->merge($items); $results = $results->concat($items);
} }
} }
@@ -216,7 +224,7 @@ class GlobalSearchApiController extends Controller
$formatted['type'] = 'bylaw'; $formatted['type'] = 'bylaw';
return $formatted; return $formatted;
}); });
$results = $results->merge($bylaws); $results = $results->concat($bylaws);
// 2. Judicial Precedents // 2. Judicial Precedents
$judicialQuery = JudicialPrecedent::with('category') $judicialQuery = JudicialPrecedent::with('category')
@@ -231,7 +239,7 @@ class GlobalSearchApiController extends Controller
$formatted['type'] = 'judicial_precedent'; $formatted['type'] = 'judicial_precedent';
return $formatted; return $formatted;
}); });
$results = $results->merge($judicials); $results = $results->concat($judicials);
// 3. Legal Opinions // 3. Legal Opinions
$opinionQuery = LegalOpinion::with('category') $opinionQuery = LegalOpinion::with('category')
@@ -247,14 +255,21 @@ class GlobalSearchApiController extends Controller
$formatted['type'] = 'legal_opinion'; $formatted['type'] = 'legal_opinion';
return $formatted; return $formatted;
}); });
$results = $results->merge($opinions); $results = $results->concat($opinions);
// 4. Search Arts (Exact fast match Fash search to ArtController) // 4. Search Arts
$artQuery = Art::with(['law', 'law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate']) $artQuery = Art::with(['law', '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}%")
->orWhere('number', 'LIKE', "%{$searchTerm}%"); ->orWhere('number', '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}%"); })
->orWhereHas('gate', 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) {
@@ -262,7 +277,7 @@ class GlobalSearchApiController extends Controller
$formatted['type'] = 'art'; $formatted['type'] = 'art';
return $formatted; return $formatted;
}); });
$results = $results->merge($arts); $results = $results->concat($arts);
// 5. Structural Models // 5. Structural Models
$structuralModels = [ $structuralModels = [
@@ -286,7 +301,7 @@ class GlobalSearchApiController extends Controller
$formatted['type'] = ($table === 'section') ? 'sections' : $table; $formatted['type'] = ($table === 'section') ? 'sections' : $table;
return $formatted; return $formatted;
}); });
$results = $results->merge($items); $results = $results->concat($items);
} }
} }
} }
@@ -375,7 +390,6 @@ class GlobalSearchApiController extends Controller
$text = $art->text ?? ''; $text = $art->text ?? '';
// Multi-byte safe Context Snippet Extraction for Persian/Arabic Fash search Fash search Fash search
if (!empty($searchTerm) && !empty($text)) { if (!empty($searchTerm) && !empty($text)) {
$pos = mb_stripos($text, $searchTerm); $pos = mb_stripos($text, $searchTerm);
if ($pos !== false) { if ($pos !== false) {
@@ -391,7 +405,6 @@ class GlobalSearchApiController extends Controller
$context = $text; $context = $text;
} }
// PERFECTLY MATCHED TO YOUR ARTCONTROLLER MAPPING
return [ return [
'id' => $art->id, 'id' => $art->id,
'title' => $art->title, 'title' => $art->title,
@@ -406,7 +419,6 @@ class GlobalSearchApiController extends Controller
'price' => $law?->price, 'price' => $law?->price,
'image' => $law?->image, 'image' => $law?->image,
// Relational Objects
'law' => $art->law != null ? [ 'law' => $art->law != null ? [
'id' => $art->law->id, 'id' => $art->law->id,
'title' => $art->law->title, 'title' => $art->law->title,