globalscope wilayah

This commit is contained in:
2026-05-06 09:06:37 +07:00
parent 3c6680c71c
commit 54f06af30b
5 changed files with 94 additions and 1 deletions
+23
View File
@@ -8,6 +8,7 @@ use App\Models\Wilayah\KabupatenKota;
use App\Models\Wilayah\DesaKelurahan;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Builder;
class Kecamatan extends Model
{
@@ -30,4 +31,26 @@ class Kecamatan extends Model
{
return $this->hasMany(DesaKelurahan::class, 'kecamatan_id');
}
protected static function booted()
{
static::addGlobalScope('program', function (Builder $builder) {
if (!app()->has('SystemProgram')) {
return;
}
$programId = app('SystemProgram')->id;
$builder->where(function ($query) use ($programId) {
$query->whereIn('id', function ($q) use ($programId) {
$q->select('kecamatan_id')
->from('core_program_lokasi')
->where('program_id', $programId)
->whereNotNull('kecamatan_id');
});
});
});
}
}