done fixing search -> next review

This commit is contained in:
JokoPrasetio 2026-01-27 15:45:20 +07:00
parent c005da49af
commit 8c1663f9c3

View File

@ -97,6 +97,7 @@ class DashboardController extends Controller
$filterUnit = request('unitKerja'); $filterUnit = request('unitKerja');
$subUnit = request('subUnit'); $subUnit = request('subUnit');
$keyword = request('keyword'); $keyword = request('keyword');
$hasKeyword = !empty($keyword);
$subArray = $subUnit ? explode(',', $subUnit) : []; $subArray = $subUnit ? explode(',', $subUnit) : [];
$katArray = $kategori ? explode(',', $kategori) : []; $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(); $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) { } elseif ($aksesAll) {
/* all akses */ /* all akses */
$unitKerja = UnitKerja::where('statusenabled', true)->with([ // muat relasi $unitKerja = UnitKerja::where('statusenabled', true)
'subUnitKerja' => fn($q) => $q->with([ // sub-unit ->when($hasKeyword, fn($q) => $q->whereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f)))
'fileDirectory' => fn($f) => $f->whereNotNull('status_action')->when($keyword, fn($q) => ->with([ // muat relasi
$q->where('file', 'ilike', "%{$keyword}%") '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') ->select('id', 'name')
->get(); ->get();
} elseif ($allowedUnitIds) { } elseif ($allowedUnitIds) {
$unitKerja = UnitKerja::where('statusenabled', true) $unitKerja = UnitKerja::where('statusenabled', true)
->when($hasKeyword, fn($q) => $q->whereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f)))
->where(function ($q) use ($allowedUnitIds, $applyFileQuery) { ->where(function ($q) use ($allowedUnitIds, $applyFileQuery) {
if ($allowedUnitIds && $allowedUnitIds->isNotEmpty()) { if ($allowedUnitIds && $allowedUnitIds->isNotEmpty()) {
$q->whereIn('id', $allowedUnitIds); $q->whereIn('id', $allowedUnitIds);
@ -181,6 +185,7 @@ class DashboardController extends Controller
} else { } else {
$unitKerja = UnitKerja::where('statusenabled', true) $unitKerja = UnitKerja::where('statusenabled', true)
->when($hasKeyword, fn($q) => $q->whereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f)))
->where(function ($q) use ($authUnit, $applyFileQuery) { ->where(function ($q) use ($authUnit, $applyFileQuery) {
if ($authUnit) { if ($authUnit) {
$q->where('id', $authUnit); $q->where('id', $authUnit);
@ -188,8 +193,8 @@ class DashboardController extends Controller
$q->orWhereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f)); $q->orWhereHas('subUnitKerja.fileDirectory', fn($f) => $applyFileQuery($f));
}) })
->with([ // 1. sub-unit milik user ->with([ // 1. sub-unit milik user
'subUnitKerja' => fn($q) => $q->where(function ($sq) use ($authSub, $applyFileQuery) { 'subUnitKerja' => fn($q) => $q->where(function ($sq) use ($authSub, $applyFileQuery, $hasKeyword) {
if ($authSub) { if ($authSub && !$hasKeyword) {
$sq->where('id', $authSub); $sq->where('id', $authSub);
} }
$sq->orWhereHas('fileDirectory', fn($f) => $applyFileQuery($f)); $sq->orWhereHas('fileDirectory', fn($f) => $applyFileQuery($f));