feat: add app and database modules

This commit is contained in:
2026-05-21 16:05:11 +07:00
parent 37b7e783f5
commit fad70d096b
212 changed files with 23901 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
<?php
namespace App\Traits;
use Illuminate\Support\Facades\DB;
trait HasAutoCode
{
/**
* Boot the trait.
* Auto-generate code before creating record.
*/
protected static function bootHasAutoCode(): void
{
static::creating(function ($model) {
// Skip if code already set (manual / seeder)
if (! empty($model->code)) {
return;
}
$prefix = $model->getCodePrefix();
$column = $model->getCodeColumn();
$length = $model->getCodeLength();
$lastCode = DB::table($model->getTable())
->where($column, 'like', $prefix.'%')
->orderBy($column, 'desc')
->value($column);
$nextNumber = 1;
if ($lastCode) {
$nextNumber = (int) substr($lastCode, strlen($prefix)) + 1;
}
$model->{$column} = $prefix.str_pad($nextNumber, $length, '0', STR_PAD_LEFT);
});
}
/**
* Prefix code (WAJIB override di model)
*/
abstract protected function getCodePrefix(): string;
/**
* Code column name
*/
protected function getCodeColumn(): string
{
return 'code';
}
/**
* Numeric length
*/
protected function getCodeLength(): int
{
return 3;
}
}