diff --git a/app/Http/Controllers/api/BylawApiController.php b/app/Http/Controllers/api/BylawApiController.php index e7aaf28..16dbf13 100644 --- a/app/Http/Controllers/api/BylawApiController.php +++ b/app/Http/Controllers/api/BylawApiController.php @@ -51,7 +51,7 @@ class BylawApiController extends Controller 'per_page' => 'nullable|integer|min:1|max:100', ]); - $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) + $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true); if ($request->filled('category_id')) { @@ -96,7 +96,7 @@ class BylawApiController extends Controller public function single($id) { - $bylaw = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) + $bylaw = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->find($id); @@ -160,7 +160,7 @@ class BylawApiController extends Controller 'per_page' => 'nullable|integer|min:1|max:100', ]); - $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) + $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true); if ($request->filled('search')) { @@ -171,8 +171,10 @@ class BylawApiController extends Controller ->orWhere('description', 'LIKE', "%{$searchTerm}%") ->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%") ->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%") - ->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) { - $nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%"); + ->orWhereIn('id', function($subQuery) use ($searchTerm) { + $subQuery->select('document_id') + ->from('bylaw_nodes') + ->where('node_content', 'LIKE', "%{$searchTerm}%"); }); }); } @@ -209,7 +211,7 @@ class BylawApiController extends Controller { $searchTerm = $request->input('search'); - $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) + $query = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true); if (!empty($searchTerm)) { @@ -218,8 +220,10 @@ class BylawApiController extends Controller ->orWhere('description', 'LIKE', "%{$searchTerm}%") ->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%") ->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%") - ->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) { - $nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%"); + ->orWhereIn('id', function($subQuery) use ($searchTerm) { + $subQuery->select('document_id') + ->from('bylaw_nodes') + ->where('node_content', 'LIKE', "%{$searchTerm}%"); }); }); } @@ -237,7 +241,7 @@ class BylawApiController extends Controller public function pager($id) { - $current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) + $current = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->find($id); @@ -248,7 +252,7 @@ class BylawApiController extends Controller ], 404); } - $previousItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) + $previousItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->where(function ($query) use ($current) { if ($current->approval_date) { @@ -265,7 +269,7 @@ class BylawApiController extends Controller ->orderBy('id', 'desc') ->first(); - $nextItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) + $nextItem = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->where(function ($query) use ($current) { if ($current->approval_date) { @@ -303,10 +307,15 @@ class BylawApiController extends Controller { $isLocked = $this->isLockedForCurrentUser($item); - // Map description: If description is null, combine all node_content into one full text! $description = $item->description; - if (empty($description) && $item->relationLoaded('nodes')) { - $description = $item->nodes->pluck('node_content')->filter()->implode("\n\n"); + + // Direct Query Fallback: If description is empty, fetch node_content manually + if (empty($description)) { + $description = BylawNode::where('document_id', $item->id) + ->orderBy('sort_order', 'asc') + ->pluck('node_content') + ->filter() + ->implode("\n\n"); } return [ diff --git a/app/Http/Controllers/api/GlobalSearchApiController.php b/app/Http/Controllers/api/GlobalSearchApiController.php index 4fd122e..662086b 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; @@ -46,16 +47,18 @@ class GlobalSearchApiController extends Controller if (!empty($searchTerm)) { $limitPerTable = 100; // Performance safeguard - // 1. Search Bylaw Documents (Including deeper node_content search) - $bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority', 'nodes']) + // 1. Search Bylaw Documents (Including node_content subquery) + $bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority']) ->where('is_published', true) ->where(function ($q) use ($searchTerm) { $q->where('title', 'LIKE', "%{$searchTerm}%") ->orWhere('description', 'LIKE', "%{$searchTerm}%") ->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%") ->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%") - ->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) { - $nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%"); + ->orWhereIn('id', function($subQuery) use ($searchTerm) { + $subQuery->select('document_id') + ->from('bylaw_nodes') + ->where('node_content', 'LIKE', "%{$searchTerm}%"); }); })->take($limitPerTable)->get()->map(function($item) { $formatted = $this->formatBylaw($item); @@ -191,15 +194,17 @@ 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}%") ->orWhere('description', 'LIKE', "%{$searchTerm}%") ->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%") ->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%") - ->orWhereHas('nodes', function ($nodeQuery) use ($searchTerm) { - $nodeQuery->where('node_content', 'LIKE', "%{$searchTerm}%"); + ->orWhereIn('id', function($subQuery) use ($searchTerm) { + $subQuery->select('document_id') + ->from('bylaw_nodes') + ->where('node_content', 'LIKE', "%{$searchTerm}%"); }); }); $totalCount += $bylawQuery->count(); @@ -299,10 +304,15 @@ class GlobalSearchApiController extends Controller { $isLocked = $this->isLockedForCurrentUserGeneric($item); - // Fallback description to node_content if empty $description = $item->description; - if (empty($description) && $item->relationLoaded('nodes')) { - $description = $item->nodes->pluck('node_content')->filter()->implode("\n\n"); + + // Direct Query Fallback: If description is empty, fetch node_content manually + if (empty($description)) { + $description = BylawNode::where('document_id', $item->id) + ->orderBy('sort_order', 'asc') + ->pluck('node_content') + ->filter() + ->implode("\n\n"); } return [