diff --git a/app/Models/ProvinceCore.php b/app/Models/ProvinceCore.php new file mode 100644 index 0000000..b6d078f --- /dev/null +++ b/app/Models/ProvinceCore.php @@ -0,0 +1,161 @@ + 'datetime', + 'updated_at' => 'datetime', + ]; + + /** + * Scope untuk mencari berdasarkan kode provinsi + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param string $code + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeByCode($query, $code) + { + Log::info('ProvinceCore: Mencari provinsi dengan kode: ' . $code); + return $query->where('code', $code); + } + + /** + * Scope untuk mencari berdasarkan nama provinsi + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param string $name + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeByName($query, $name) + { + Log::info('ProvinceCore: Mencari provinsi dengan nama: ' . $name); + return $query->where('name', 'ILIKE', '%' . $name . '%'); + } + + /** + * Scope untuk mendapatkan semua provinsi yang aktif + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeActive($query) + { + Log::info('ProvinceCore: Mengambil semua provinsi aktif'); + return $query->orderBy('name', 'asc'); + } + + /** + * Mendapatkan provinsi berdasarkan kode + * + * @param string $code + * @return ProvinceCore|null + */ + public static function getByCode($code) + { + try { + Log::info('ProvinceCore: Mengambil provinsi dengan kode: ' . $code); + return self::byCode($code)->first(); + } catch (\Exception $e) { + Log::error('ProvinceCore: Error mengambil provinsi dengan kode ' . $code . ': ' . $e->getMessage()); + return null; + } + } + + /** + * Mendapatkan semua provinsi untuk dropdown/select + * + * @return \Illuminate\Database\Eloquent\Collection + */ + public static function getForDropdown() + { + try { + Log::info('ProvinceCore: Mengambil data provinsi untuk dropdown'); + return self::active()->pluck('name', 'code'); + } catch (\Exception $e) { + Log::error('ProvinceCore: Error mengambil data dropdown provinsi: ' . $e->getMessage()); + return collect(); + } + } + + /** + * Validasi kode provinsi + * + * @param string $code + * @return bool + */ + public static function isValidCode($code) + { + try { + Log::info('ProvinceCore: Validasi kode provinsi: ' . $code); + return self::byCode($code)->exists(); + } catch (\Exception $e) { + Log::error('ProvinceCore: Error validasi kode provinsi ' . $code . ': ' . $e->getMessage()); + return false; + } + } + + /** + * Boot method untuk model events + * + * @return void + */ + protected static function boot() + { + parent::boot(); + + static::creating(function ($model) { + Log::info('ProvinceCore: Membuat data provinsi baru dengan kode: ' . $model->code); + }); + + static::created(function ($model) { + Log::info('ProvinceCore: Data provinsi berhasil dibuat dengan ID: ' . $model->id); + }); + + static::updating(function ($model) { + Log::info('ProvinceCore: Mengupdate data provinsi dengan ID: ' . $model->id); + }); + + static::updated(function ($model) { + Log::info('ProvinceCore: Data provinsi berhasil diupdate dengan ID: ' . $model->id); + }); + + static::deleting(function ($model) { + Log::info('ProvinceCore: Menghapus data provinsi dengan ID: ' . $model->id); + }); + + static::deleted(function ($model) { + Log::info('ProvinceCore: Data provinsi berhasil dihapus dengan ID: ' . $model->id); + }); + } +} \ No newline at end of file diff --git a/database/migrations/2025_07_10_025454_create_province_core_table.php b/database/migrations/2025_07_10_025454_create_province_core_table.php new file mode 100644 index 0000000..71f601a --- /dev/null +++ b/database/migrations/2025_07_10_025454_create_province_core_table.php @@ -0,0 +1,64 @@ +id(); + $table->string('code', 10)->unique()->comment('Kode provinsi unik'); + $table->string('name', 255)->comment('Nama provinsi'); + $table->timestamps(); + + // Index untuk performa pencarian + $table->index(['code']); + $table->index(['name']); + }); + + DB::commit(); + Log::info('Migration province_core table berhasil dibuat'); + + } catch (\Exception $e) { + DB::rollback(); + Log::error('Migration province_core table gagal: ' . $e->getMessage()); + throw $e; + } + } + + /** + * Membalikkan migrasi dengan menghapus tabel province_core + * + * @return void + */ + public function down(): void + { + DB::beginTransaction(); + + try { + Schema::dropIfExists('province_core'); + + DB::commit(); + Log::info('Migration rollback province_core table berhasil'); + + } catch (\Exception $e) { + DB::rollback(); + Log::error('Migration rollback province_core table gagal: ' . $e->getMessage()); + throw $e; + } + } +}; \ No newline at end of file