globalscope wilayah
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Models\Wilayah\Provinsi;
|
||||
use App\Models\Wilayah\Kecamatan;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class KabupatenKota extends Model
|
||||
{
|
||||
@@ -31,4 +32,26 @@ class KabupatenKota extends Model
|
||||
{
|
||||
return $this->hasMany(Kecamatan::class, 'kabupaten_kota_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('kabupaten_kota_id')
|
||||
->from('core_program_lokasi')
|
||||
->where('program_id', $programId)
|
||||
->whereNotNull('kabupaten_kota_id');
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user