Fixed api issue

This commit is contained in:
m.razaghi
2026-07-11 12:25:46 +03:30
parent 3b9fcd76df
commit ffea0e3c11
2 changed files with 43 additions and 24 deletions

View File

@@ -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 [