feat(user): membuat skema table users dan employees dan menambahkan enum untuk kebutuhan data option
This commit is contained in:
parent
865edfb618
commit
49f71d3683
33
app/Enums/EmploymentStatus.php
Normal file
33
app/Enums/EmploymentStatus.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
enum EmploymentStatus: int
|
||||||
|
{
|
||||||
|
case PERMANENT = 1;
|
||||||
|
case CONTRACT = 2;
|
||||||
|
case INTERN = 3;
|
||||||
|
|
||||||
|
public function label(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::PERMANENT => 'Permanent',
|
||||||
|
self::CONTRACT => 'Contract',
|
||||||
|
self::INTERN => 'Intern',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function color(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::PERMANENT => 'primary',
|
||||||
|
self::CONTRACT => 'warning',
|
||||||
|
self::INTERN => 'secondary',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function values(): array
|
||||||
|
{
|
||||||
|
return array_map(fn ($case) => $case->value, self::cases());
|
||||||
|
}
|
||||||
|
}
|
||||||
30
app/Enums/Gender.php
Normal file
30
app/Enums/Gender.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
enum Gender: int
|
||||||
|
{
|
||||||
|
case MALE = 1;
|
||||||
|
case FEMALE = 0;
|
||||||
|
|
||||||
|
public function label(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::MALE => 'Male',
|
||||||
|
self::FEMALE => 'Female',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function color(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::MALE => 'primary',
|
||||||
|
self::FEMALE => 'success',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function values(): array
|
||||||
|
{
|
||||||
|
return array_map(fn ($case) => $case->value, self::cases());
|
||||||
|
}
|
||||||
|
}
|
||||||
33
app/Enums/UserStatus.php
Normal file
33
app/Enums/UserStatus.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Enums;
|
||||||
|
|
||||||
|
enum UserStatus: int
|
||||||
|
{
|
||||||
|
case ACTIVE = 1;
|
||||||
|
case INACTIVE = 2;
|
||||||
|
case SUSPENDED = 3;
|
||||||
|
|
||||||
|
public function label(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::ACTIVE => 'Active',
|
||||||
|
self::INACTIVE => 'Inactive',
|
||||||
|
self::SUSPENDED => 'Suspended',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function color(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::ACTIVE => 'primary',
|
||||||
|
self::INACTIVE => 'warning',
|
||||||
|
self::SUSPENDED => 'danger',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function values(): array
|
||||||
|
{
|
||||||
|
return array_map(fn ($case) => $case->value, self::cases());
|
||||||
|
}
|
||||||
|
}
|
||||||
33
app/Models/Employee.php
Normal file
33
app/Models/Employee.php
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\EmploymentStatus;
|
||||||
|
use App\Enums\Gender;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class Employee extends Model
|
||||||
|
{
|
||||||
|
use HasFactory, SoftDeletes;
|
||||||
|
|
||||||
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'gender' => Gender::class,
|
||||||
|
'birthdate' => 'date',
|
||||||
|
'hire_date' => 'date',
|
||||||
|
'resign_date' => 'date',
|
||||||
|
'status' => EmploymentStatus::class,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,46 +3,31 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||||
|
|
||||||
|
use App\Enums\UserStatus;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
|
|
||||||
class User extends Authenticatable
|
class User extends Authenticatable
|
||||||
{
|
{
|
||||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
use HasFactory, Notifiable, SoftDeletes;
|
||||||
use HasFactory, Notifiable;
|
|
||||||
|
|
||||||
/**
|
protected $guarded = ['id'];
|
||||||
* The attributes that are mass assignable.
|
|
||||||
*
|
|
||||||
* @var list<string>
|
|
||||||
*/
|
|
||||||
protected $fillable = [
|
|
||||||
'name',
|
|
||||||
'email',
|
|
||||||
'password',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The attributes that should be hidden for serialization.
|
|
||||||
*
|
|
||||||
* @var list<string>
|
|
||||||
*/
|
|
||||||
protected $hidden = [
|
|
||||||
'password',
|
|
||||||
'remember_token',
|
|
||||||
];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the attributes that should be cast.
|
|
||||||
*
|
|
||||||
* @return array<string, string>
|
|
||||||
*/
|
|
||||||
protected function casts(): array
|
protected function casts(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'email_verified_at' => 'datetime',
|
'last_login_at' => 'datetime',
|
||||||
'password' => 'hashed',
|
'last_password_change_at' => 'datetime',
|
||||||
|
'status' => UserStatus::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function employee(): HasOne
|
||||||
|
{
|
||||||
|
return $this->hasOne(Employee::class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Enums\UserStatus;
|
||||||
use Illuminate\Database\Migrations\Migration;
|
use Illuminate\Database\Migrations\Migration;
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
use Illuminate\Support\Facades\Schema;
|
use Illuminate\Support\Facades\Schema;
|
||||||
@ -13,12 +14,15 @@ public function up(): void
|
|||||||
{
|
{
|
||||||
Schema::create('users', function (Blueprint $table) {
|
Schema::create('users', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->id();
|
||||||
$table->string('name');
|
$table->string('email', 100)->unique();
|
||||||
$table->string('email')->unique();
|
$table->string('username', 20)->unique();
|
||||||
$table->timestamp('email_verified_at')->nullable();
|
$table->string('phone_number', 20);
|
||||||
$table->string('password');
|
$table->string('password', 97);
|
||||||
$table->rememberToken();
|
$table->timestamp('last_login_at')->nullable();
|
||||||
|
$table->timestamp('last_password_change_at')->nullable();
|
||||||
|
$table->enum('status', [UserStatus::values()])->default(UserStatus::INACTIVE);
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
$table->softDeletes();
|
||||||
});
|
});
|
||||||
|
|
||||||
Schema::create('password_reset_tokens', function (Blueprint $table) {
|
Schema::create('password_reset_tokens', function (Blueprint $table) {
|
||||||
|
|||||||
@ -0,0 +1,40 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Enums\EmploymentStatus;
|
||||||
|
use App\Enums\Gender;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('employees', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||||
|
$table->string('code', 50)->unique();
|
||||||
|
$table->string('full_name', 100);
|
||||||
|
$table->enum('gender', [Gender::values()]);
|
||||||
|
$table->date('birthdate');
|
||||||
|
$table->date('hire_date');
|
||||||
|
$table->date('resign_date')->nullable();
|
||||||
|
$table->enum('status', [EmploymentStatus::values()])->default(EmploymentStatus::PERMANENT);
|
||||||
|
$table->unsignedInteger('base_salary')->default(0);
|
||||||
|
$table->text('address');
|
||||||
|
$table->timestamps();
|
||||||
|
$table->softDeletes();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('employees');
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user