Skip to content

Commit

Permalink
user columns, seeder, and factory
Browse files Browse the repository at this point in the history
  • Loading branch information
iqbaleff214 committed May 31, 2024
1 parent 9d7f27b commit 82d012c
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 11 deletions.
9 changes: 9 additions & 0 deletions app/Enum/Gender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace App\Enum;

enum Gender: string
{
case MALE = 'male';
case FEMALE = 'female';
}
11 changes: 11 additions & 0 deletions app/Enum/MaritalStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Enum;

enum MaritalStatus: string
{
case SINGLE = 'single';
case MARRIED = 'married';
case DIVORCED = 'divorced';
case WIDOWED = 'widowed';
}
12 changes: 12 additions & 0 deletions app/Enum/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Enum;

enum Role: string
{
case OWNER = 'owner';
case HEADMASTER = 'headmaster';
case ADMINISTRATOR = 'administrator';
case TEACHER = 'teacher';
case STUDENT_GUARDIAN = 'student_guardian';
}
10 changes: 8 additions & 2 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ class User extends Authenticatable
'email',
'password',
'locale',
'role',
'image',
'phone',
'address',
'marital_status',
'gender',
];

/**
Expand All @@ -36,7 +42,7 @@ class User extends Authenticatable
];

protected $appends = [
'avatar_url',
'image_url',
];

/**
Expand All @@ -52,7 +58,7 @@ protected function casts(): array
];
}

public function avatarUrl(): Attribute
public function imageUrl(): Attribute
{
return new Attribute(
get: fn () => 'https://ui-avatars.com/api/?name='.$this->name,
Expand Down
10 changes: 9 additions & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Database\Factories;

use App\Enum\Gender;
use App\Enum\MaritalStatus;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
Expand All @@ -23,12 +25,18 @@ class UserFactory extends Factory
*/
public function definition(): array
{
$gender = fake()->randomElement(Gender::cases());

return [
'name' => fake()->name(),
'name' => fake()->name($gender->value),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'phone' => fake()->phoneNumber(),
'address' => fake()->address(),
'marital_status' => fake()->randomElement(MaritalStatus::cases()),
'gender' => $gender,
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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::table('users', function (Blueprint $table) {
$table->string('role')->default(\App\Enum\Role::STUDENT_GUARDIAN)->after('password');
$table->string('image')->nullable()->after('role');
$table->string('phone')->nullable()->after('image');
$table->string('address')->nullable()->after('phone');
$table->string('marital_status')->nullable()->after('phone');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn([
'role', 'image', 'phone', 'address', 'marital_status',
]);
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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::table('users', function (Blueprint $table) {
$table->string('gender')->after('name');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('gender');
});
}
};
9 changes: 4 additions & 5 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Database\Seeders;

use App\Enum\Role;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
Expand All @@ -13,11 +13,10 @@ class DatabaseSeeder extends Seeder
*/
public function run(): void
{
// User::factory(10)->create();

User::factory()->create([
'name' => 'Test User',
'email' => '[email protected]',
'name' => 'Tuhfah Owner',
'email' => '[email protected]',
'role' => Role::OWNER,
]);
}
}
28 changes: 28 additions & 0 deletions database/seeders/Dummy/UserSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Database\Seeders\Dummy;

use App\Enum\Role;
use App\Models\User;
use Illuminate\Database\Seeder;

class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
User::factory()->create([
'role' => Role::HEADMASTER,
]);

User::factory(10)->create([
'role' => Role::TEACHER,
]);

User::factory(100)->create([
'role' => Role::STUDENT_GUARDIAN,
]);
}
}
9 changes: 9 additions & 0 deletions lang/en/label.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@
'cancel' => 'Cancel',
'new' => 'New',
'detail' => 'Detail',
'owner' => 'Owner',
'headmaster' => 'Headmaster',
'administrator' => 'Administrator',
'teacher' => 'Teacher',
'student_guardian' => 'Student Guardian',
'single' => 'Single',
'married' => 'Married',
'divorced' => 'Divorced',
'widowed' => 'Widowed',
];
9 changes: 9 additions & 0 deletions lang/id/label.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@
'cancel' => 'Batalkan',
'new' => 'Baru',
'detail' => 'Detail',
'owner' => 'Pemilik',
'headmaster' => 'Kepala TPQ/TPA',
'administrator' => 'Admin',
'teacher' => 'Pengajar',
'student_guardian' => 'Wali Santri',
'single' => 'Belum Kawin',
'married' => 'Kawin',
'divorced' => 'Cerai Hidup',
'widowed' => 'Cerai Mati',
];
6 changes: 3 additions & 3 deletions resources/views/components/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class="github-button"
<li class="nav-item navbar-dropdown dropdown-user dropdown">
<a class="nav-link dropdown-toggle hide-arrow" href="javascript:void(0);" data-bs-toggle="dropdown">
<div class="avatar avatar-online">
<img src="{{ auth()->user()->avatar_url }}" alt class="w-px-40 h-px-40 rounded-circle" />
<img src="{{ auth()->user()->image_url }}" alt class="w-px-40 h-px-40 rounded-circle" />
</div>
</a>
<ul class="dropdown-menu dropdown-menu-end">
Expand All @@ -51,12 +51,12 @@ class="github-button"
<div class="d-flex">
<div class="flex-shrink-0 me-3">
<div class="avatar avatar-online">
<img src="{{ auth()->user()->avatar_url }}" alt class="w-px-40 h-px-40 rounded-circle" />
<img src="{{ auth()->user()->image_url }}" alt class="w-px-40 h-px-40 rounded-circle" />
</div>
</div>
<div class="flex-grow-1">
<span class="fw-medium d-block">{{ auth()->user()->name }}</span>
<small class="text-muted">-</small>
<small class="text-muted">{{ __('label.' . auth()->user()->role) }}</small>
</div>
</div>
</a>
Expand Down

0 comments on commit 82d012c

Please sign in to comment.