From 8c1663f9c302b05a58041b1d46c12dedfb474dbd Mon Sep 17 00:00:00 2001 From: JokoPrasetio Date: Tue, 27 Jan 2026 15:45:20 +0700 Subject: [PATCH] done fixing search -> next review --- app/Http/Controllers/DashboardController.php | 21 ++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 3c82f8d..7018669 100644 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -97,6 +97,7 @@ class DashboardController extends Controller $filterUnit = request('unitKerja'); $subUnit = request('subUnit'); $keyword = request('keyword'); + $hasKeyword = !empty($keyword); $subArray = $subUnit ? explode(',', $subUnit) : []; $katArray = $kategori ? explode(',', $kategori) : []; $katDok = MasterKategori::when($katArray, fn($q) => $q->whereIn('master_kategori_directory_id', $katArray))->where('statusenabled', true)->select('master_kategori_directory_id', 'nama_kategori_directory')->get(); @@ -154,18 +155,21 @@ class DashboardController extends Controller } elseif ($aksesAll) { /* all akses */ - $unitKerja = UnitKerja::where('statusenabled', true)->with([ // muat relasi - 'subUnitKerja' => fn($q) => $q->with([ // sub-unit - 'fileDirectory' => fn($f) => $f->whereNotNull('status_action')->when($keyword, fn($q) => - $q->where('file', 'ilike', "%{$keyword}%") - ) - ]) + $unitKerja = UnitKerja::where('statusenabled', true) + ->when($hasKeyword, fn($q) => $q->whereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f))) + ->with([ // muat relasi + 'subUnitKerja' => fn($q) => $q + ->when($hasKeyword, fn($sq) => $sq->whereHas('fileDirectory', fn($f) => $applyFileQuery($f))) + ->with([ // sub-unit + 'fileDirectory' => fn($f) => $applyFileQuery($f) + ]) ]) ->select('id', 'name') ->get(); } elseif ($allowedUnitIds) { $unitKerja = UnitKerja::where('statusenabled', true) + ->when($hasKeyword, fn($q) => $q->whereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f))) ->where(function ($q) use ($allowedUnitIds, $applyFileQuery) { if ($allowedUnitIds && $allowedUnitIds->isNotEmpty()) { $q->whereIn('id', $allowedUnitIds); @@ -181,6 +185,7 @@ class DashboardController extends Controller } else { $unitKerja = UnitKerja::where('statusenabled', true) + ->when($hasKeyword, fn($q) => $q->whereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f))) ->where(function ($q) use ($authUnit, $applyFileQuery) { if ($authUnit) { $q->where('id', $authUnit); @@ -188,8 +193,8 @@ class DashboardController extends Controller $q->orWhereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f)); }) ->with([ // 1. sub-unit milik user - 'subUnitKerja' => fn($q) => $q->where(function ($sq) use ($authSub, $applyFileQuery) { - if ($authSub) { + 'subUnitKerja' => fn($q) => $q->where(function ($sq) use ($authSub, $applyFileQuery, $hasKeyword) { + if ($authSub && !$hasKeyword) { $sq->where('id', $authSub); } $sq->orWhereHas('fileDirectory', fn($f) => $applyFileQuery($f));