diff --git a/app/Http/Controllers/api/BylawApiController.php b/app/Http/Controllers/api/BylawApiController.php index 16dbf13..3527c87 100644 --- a/app/Http/Controllers/api/BylawApiController.php +++ b/app/Http/Controllers/api/BylawApiController.php @@ -10,6 +10,7 @@ use App\Models\BylawNode; use App\Models\BylawCodificationLink; use App\Traits\BaseApiResponse; use Illuminate\Http\Request; +use Illuminate\Support\Facades\DB; class BylawApiController extends Controller { @@ -309,13 +310,18 @@ class BylawApiController extends Controller $description = $item->description; - // Direct Query Fallback: If description is empty, fetch node_content manually - if (empty($description)) { - $description = BylawNode::where('document_id', $item->id) + // Check if description is practically empty (null, empty string, or just empty WYSIWYG HTML like

) + $cleanDesc = trim(strip_tags($description ?? '')); + + // Direct Query Fallback: Fetch node_content manually via DB facade to bypass any Model restrictions + if (empty($cleanDesc)) { + $nodeContents = DB::table('bylaw_nodes') + ->where('document_id', $item->id) ->orderBy('sort_order', 'asc') ->pluck('node_content') - ->filter() - ->implode("\n\n"); + ->toArray(); + + $description = implode("\n\n", array_filter($nodeContents)); } return [ diff --git a/app/Http/Controllers/api/GlobalSearchApiController.php b/app/Http/Controllers/api/GlobalSearchApiController.php index 66b868d..405b40c 100644 --- a/app/Http/Controllers/api/GlobalSearchApiController.php +++ b/app/Http/Controllers/api/GlobalSearchApiController.php @@ -7,7 +7,6 @@ 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; @@ -20,6 +19,7 @@ use App\Models\Volum; use App\Traits\BaseApiResponse; use Illuminate\Http\Request; use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Support\Facades\DB; class GlobalSearchApiController extends Controller { @@ -48,7 +48,7 @@ class GlobalSearchApiController extends Controller $limitPerTable = 100; // Performance safeguard // 1. Search Bylaw Documents - $bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) + $bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->where(function ($q) use ($searchTerm) { $q->where('title', 'LIKE', "%{$searchTerm}%") @@ -94,8 +94,8 @@ class GlobalSearchApiController extends Controller }); $results = $results->merge($opinions); - // 4. Search Arts (Deep Relational Search & Eager Loading) - $arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate', 'division', 'branch']) + // 4. Search Arts + $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}%") @@ -202,7 +202,7 @@ class GlobalSearchApiController extends Controller if (!empty($searchTerm)) { // 1. Bylaw Documents - $bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) + $bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->where(function ($q) use ($searchTerm) { $q->where('title', 'LIKE', "%{$searchTerm}%") @@ -255,7 +255,7 @@ class GlobalSearchApiController extends Controller $results = $results->merge($opinions); // 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) { $q->where('title', 'LIKE', "%{$searchTerm}%") ->orWhere('text', 'LIKE', "%{$searchTerm}%") @@ -321,8 +321,19 @@ class GlobalSearchApiController extends Controller $isLocked = $this->isLockedForCurrentUserGeneric($item); $description = $item->description; - if (empty($description) && $item->relationLoaded('nodes')) { - $description = $item->nodes->pluck('node_content')->filter()->implode("\n\n"); + + // Check if description is practically empty (null, empty string, or just empty WYSIWYG HTML like

) + $cleanDesc = trim(strip_tags($description ?? '')); + + // Direct Query Fallback: Fetch node_content manually via DB facade to bypass any Model restrictions + if (empty($cleanDesc)) { + $nodeContents = DB::table('bylaw_nodes') + ->where('document_id', $item->id) + ->orderBy('sort_order', 'asc') + ->pluck('node_content') + ->toArray(); + + $description = implode("\n\n", array_filter($nodeContents)); } return [ @@ -446,7 +457,6 @@ class GlobalSearchApiController extends Controller 'number' => $item->number, 'is_locked' => $isLocked, 'route' => array_values($this->route($modelClass, $item)), - // Keep law payload explicitly as an object to prevent DTO parsing errors 'law' => $law ? [ 'id' => $law->id, 'title' => $law->title, @@ -464,18 +474,17 @@ class GlobalSearchApiController extends Controller private function route($modelClass, $query) { - // Now safely uses eager loaded relations if they exist! if ((new $modelClass)->getTable() == 'art') { $route = array_filter([ - $query->law->title ?? Law::find($query->law_id)?->title, - $query->volum->title ?? Volum::find($query->volum_id)?->title, - $query->book->title ?? Book::find($query->book_id)?->title, - $query->division->title ?? Division::find($query->division_id)?->title, - $query->section->title ?? Section::find($query->section_id)?->title, - $query->chapter->title ?? Chapter::find($query->chapter_id)?->title, - $query->part->title ?? Part::find($query->part_id)?->title, - $query->gate->title ?? Gate::find($query->gate_id)?->title, - $query->branch->title ?? Branch::find($query->branch_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('book') ? $query->book?->title : Book::find($query->book_id)?->title, + Division::find($query->division_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('part') ? $query->part?->title : Part::find($query->part_id)?->title, + $query->relationLoaded('gate') ? $query->gate?->title : Gate::find($query->gate_id)?->title, + Branch::find($query->branch_id)?->title, ]); return array_values($route); }