feat: Create Lecturer resource with CRUD functionality, including pages for listing, creating, and editing lecturers, along with a form schema and migration for managing lecturer data in the admin panel.
This commit is contained in:
parent
5e63ef2744
commit
c9ad3b272c
52
app/Filament/Resources/Master/Lecturers/LecturerResource.php
Normal file
52
app/Filament/Resources/Master/Lecturers/LecturerResource.php
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Master\Lecturers;
|
||||||
|
|
||||||
|
use App\Filament\Resources\Master\Lecturers\Pages\CreateLecturer;
|
||||||
|
use App\Filament\Resources\Master\Lecturers\Pages\EditLecturer;
|
||||||
|
use App\Filament\Resources\Master\Lecturers\Pages\ListLecturers;
|
||||||
|
use App\Filament\Resources\Master\Lecturers\Schemas\LecturerForm;
|
||||||
|
use App\Filament\Resources\Master\Lecturers\Tables\LecturersTable;
|
||||||
|
use App\Models\Lecturer;
|
||||||
|
use BackedEnum;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
use Filament\Support\Icons\Heroicon;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use UnitEnum;
|
||||||
|
|
||||||
|
class LecturerResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = Lecturer::class;
|
||||||
|
|
||||||
|
protected static string|UnitEnum|null $navigationGroup = 'Master';
|
||||||
|
|
||||||
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedUserGroup;
|
||||||
|
|
||||||
|
protected static ?string $navigationLabel = 'Dosen';
|
||||||
|
|
||||||
|
protected static ?int $navigationSort = 10;
|
||||||
|
|
||||||
|
protected static ?string $recordTitleAttribute = 'full_name';
|
||||||
|
|
||||||
|
protected static ?string $slug = 'master/lecturers';
|
||||||
|
|
||||||
|
public static function form(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return LecturerForm::configure($schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return LecturersTable::configure($table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => ListLecturers::route('/'),
|
||||||
|
'create' => CreateLecturer::route('/create'),
|
||||||
|
'edit' => EditLecturer::route('/{record}/edit'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,59 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Master\Lecturers\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Actions\BackAction;
|
||||||
|
use App\Filament\Resources\Master\Lecturers\LecturerResource;
|
||||||
|
use App\Filament\Support\SystemNotification;
|
||||||
|
use App\Models\User;
|
||||||
|
use Filament\Notifications\Notification;
|
||||||
|
use Filament\Resources\Pages\CreateRecord;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
|
||||||
|
class CreateLecturer extends CreateRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = LecturerResource::class;
|
||||||
|
|
||||||
|
protected ?string $heading = 'Tambah Dosen';
|
||||||
|
|
||||||
|
protected static ?string $title = 'Tambah Dosen';
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
BackAction::make()
|
||||||
|
->url(ListLecturers::getUrl()),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getRedirectUrl(): string
|
||||||
|
{
|
||||||
|
return $this->getResource()::getUrl('index');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getCreatedNotification(): ?Notification
|
||||||
|
{
|
||||||
|
return SystemNotification::create();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handleRecordCreation(array $data): Model
|
||||||
|
{
|
||||||
|
return DB::transaction(function () use ($data) {
|
||||||
|
$user = User::create([
|
||||||
|
'email' => $data['email'],
|
||||||
|
'username' => $data['username'],
|
||||||
|
'password' => Hash::make('Minimal8@'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
unset($data['email'], $data['username']);
|
||||||
|
|
||||||
|
return $this->getResource()::getModel()::create(
|
||||||
|
array_merge($data, [
|
||||||
|
'user_id' => $user->id,
|
||||||
|
])
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Master\Lecturers\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Actions\BackAction;
|
||||||
|
use App\Filament\Resources\Master\Lecturers\LecturerResource;
|
||||||
|
use App\Filament\Support\SystemNotification;
|
||||||
|
use Filament\Notifications\Notification;
|
||||||
|
use Filament\Resources\Pages\EditRecord;
|
||||||
|
|
||||||
|
class EditLecturer extends EditRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = LecturerResource::class;
|
||||||
|
|
||||||
|
protected ?string $heading = 'Ubah Dosen';
|
||||||
|
|
||||||
|
protected static ?string $title = 'Ubah Dosen';
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
BackAction::make()
|
||||||
|
->url(ListLecturers::getUrl()),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getRedirectUrl(): string
|
||||||
|
{
|
||||||
|
return $this->getResource()::getUrl('index');
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getSavedNotification(): ?Notification
|
||||||
|
{
|
||||||
|
return SystemNotification::update();
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Master\Lecturers\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Actions\Cheerful\CreateAction;
|
||||||
|
use App\Filament\Resources\Master\Lecturers\LecturerResource;
|
||||||
|
use Filament\Resources\Pages\ListRecords;
|
||||||
|
|
||||||
|
class ListLecturers extends ListRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = LecturerResource::class;
|
||||||
|
|
||||||
|
protected static ?string $title = 'Dosen';
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
CreateAction::make()
|
||||||
|
->label('Tambah'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
121
app/Filament/Resources/Master/Lecturers/Schemas/LecturerForm.php
Normal file
121
app/Filament/Resources/Master/Lecturers/Schemas/LecturerForm.php
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Master\Lecturers\Schemas;
|
||||||
|
|
||||||
|
use App\Enums\Sex;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Filament\Forms\Components\DatePicker;
|
||||||
|
use Filament\Forms\Components\Radio;
|
||||||
|
use Filament\Forms\Components\Textarea;
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
|
use Filament\Schemas\Components\Grid;
|
||||||
|
use Filament\Schemas\Components\Section;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
use Illuminate\Support\HtmlString;
|
||||||
|
|
||||||
|
class LecturerForm
|
||||||
|
{
|
||||||
|
public static function configure(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return $schema
|
||||||
|
->components([
|
||||||
|
Section::make('Akun')
|
||||||
|
->description(new HtmlString('Alamat Surel dan Nama Pengguna akan digunakan untuk masuk ke dalam sistem. Kata sandi bawaan adalah <code class="text-sm bg-gray-100 text-red-400 px-1.5 py-0.5 rounded font-mono">Minimal8@</code>'))
|
||||||
|
->schema([
|
||||||
|
TextInput::make('email')
|
||||||
|
->label('Alamat Surel')
|
||||||
|
->placeholder('johndoe@example.com')
|
||||||
|
->autocomplete(false)
|
||||||
|
->required()
|
||||||
|
->maxLength(254)
|
||||||
|
->email()
|
||||||
|
->unique('users', 'email', ignoreRecord: true),
|
||||||
|
|
||||||
|
TextInput::make('username')
|
||||||
|
->label('Nama Pengguna')
|
||||||
|
->placeholder('johndoe')
|
||||||
|
->autocomplete(false)
|
||||||
|
->required()
|
||||||
|
->minLength(5)
|
||||||
|
->maxLength(20)
|
||||||
|
->regex('/^[a-zA-Z0-9_.-]+$/')
|
||||||
|
->unique('users', 'username', ignoreRecord: true),
|
||||||
|
])
|
||||||
|
->columns(2)
|
||||||
|
->hiddenOn('edit'),
|
||||||
|
|
||||||
|
Section::make('Informasi Dosen')
|
||||||
|
->schema([
|
||||||
|
TextInput::make('full_name')
|
||||||
|
->label('Nama Lengkap')
|
||||||
|
->placeholder('John Doe')
|
||||||
|
->autocomplete(false)
|
||||||
|
->required()
|
||||||
|
->maxLength(100)
|
||||||
|
->minLength(3)
|
||||||
|
->autofocus()
|
||||||
|
->helperText('Masukkan nama lengkap dosen beserta gelar akademik.')
|
||||||
|
->columnSpanFull(),
|
||||||
|
|
||||||
|
Grid::make(3)
|
||||||
|
->schema([
|
||||||
|
TextInput::make('lecturer_identification_number')
|
||||||
|
->label('NIDN')
|
||||||
|
->placeholder('1234567890')
|
||||||
|
->autocomplete(false)
|
||||||
|
->required()
|
||||||
|
->numeric()
|
||||||
|
->rules(['digits:10'])
|
||||||
|
->unique(ignoreRecord: true),
|
||||||
|
|
||||||
|
TextInput::make('phone_number')
|
||||||
|
->label('Nomor Telepon')
|
||||||
|
->placeholder('0812 3456 78910')
|
||||||
|
->autocomplete(false)
|
||||||
|
->required()
|
||||||
|
->maxLength(20)
|
||||||
|
->minLength(10)
|
||||||
|
->rules(['regex:/^[0-9 ]+$/'])
|
||||||
|
->mask('9999 9999 99999'),
|
||||||
|
|
||||||
|
Radio::make('sex')
|
||||||
|
->label('Jenis Kelamin')
|
||||||
|
->options(Sex::class)
|
||||||
|
->required()
|
||||||
|
->inline(),
|
||||||
|
])
|
||||||
|
->columnSpanFull(),
|
||||||
|
|
||||||
|
Textarea::make('address')
|
||||||
|
->label('Alamat')
|
||||||
|
->placeholder('Jl. Contoh No. 123, Kota, Provinsi')
|
||||||
|
->autocomplete(false)
|
||||||
|
->rows(3)
|
||||||
|
->nullable()
|
||||||
|
->columnSpanFull(),
|
||||||
|
|
||||||
|
Grid::make(2)
|
||||||
|
->schema([
|
||||||
|
DatePicker::make('date_of_birth')
|
||||||
|
->label('Tanggal Lahir')
|
||||||
|
->placeholder('Pilih Tanggal')
|
||||||
|
->required()
|
||||||
|
->native(false)
|
||||||
|
->displayFormat('l, d F Y')
|
||||||
|
->maxDate(Carbon::now()->subYears(25)),
|
||||||
|
|
||||||
|
TextInput::make('place_of_birth')
|
||||||
|
->label('Tempat Lahir')
|
||||||
|
->placeholder('Purwakarta')
|
||||||
|
->autocomplete(false)
|
||||||
|
->required()
|
||||||
|
->maxLength(50)
|
||||||
|
->minLength(3),
|
||||||
|
])
|
||||||
|
->columnSpanFull(),
|
||||||
|
])
|
||||||
|
->columns(2),
|
||||||
|
])
|
||||||
|
->columns(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,113 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Master\Lecturers\Tables;
|
||||||
|
|
||||||
|
use App\Enums\Sex;
|
||||||
|
use App\Filament\Actions\Cheerful\DeleteAction;
|
||||||
|
use App\Filament\Actions\Cheerful\EditAction;
|
||||||
|
use App\Filament\Actions\Cheerful\ForceDeleteAction;
|
||||||
|
use App\Filament\Actions\Cheerful\RestoreAction;
|
||||||
|
use App\Filament\Actions\DefaultBulkActions;
|
||||||
|
use App\Filament\Columns\TimestampColumns;
|
||||||
|
use App\Models\Lecturer;
|
||||||
|
use Filament\Actions\BulkActionGroup;
|
||||||
|
use Filament\Support\Enums\Width;
|
||||||
|
use Filament\Support\Icons\Heroicon;
|
||||||
|
use Filament\Tables\Columns\TextColumn;
|
||||||
|
use Filament\Tables\Filters\SelectFilter;
|
||||||
|
use Filament\Tables\Filters\TrashedFilter;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
|
class LecturersTable
|
||||||
|
{
|
||||||
|
public static function configure(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
TextColumn::make('user.email')
|
||||||
|
->label('Akun')
|
||||||
|
->formatStateUsing(fn ($state, Lecturer $record) => "
|
||||||
|
<div class='flex flex-col gap-1'>
|
||||||
|
<div><span class='font-bold text-gray-500 text-xs uppercase'>Alamat Surel:</span> <span class='font-medium'>{$state}</span></div>
|
||||||
|
<div><span class='font-bold text-gray-500 text-xs uppercase'>Nama Pengguna:</span> <span class='font-medium'>{$record->user?->username}</span></div>
|
||||||
|
</div>
|
||||||
|
")
|
||||||
|
->html()
|
||||||
|
->searchable(['email', 'username'])
|
||||||
|
->sortable(),
|
||||||
|
|
||||||
|
TextColumn::make('full_name')
|
||||||
|
->label('Nama Lengkap')
|
||||||
|
->searchable(query: function (Builder $query, string $search) {
|
||||||
|
$query->where('full_name', 'like', "%{$search}%")
|
||||||
|
->orWhere('lecturer_identification_number', 'like', "%{$search}%");
|
||||||
|
})
|
||||||
|
->sortable()
|
||||||
|
->description(fn (Lecturer $record) => $record->lecturer_identification_number),
|
||||||
|
|
||||||
|
TextColumn::make('phone_number')
|
||||||
|
->label('No. Telepon')
|
||||||
|
->searchable()
|
||||||
|
->sortable(),
|
||||||
|
|
||||||
|
TextColumn::make('sex')
|
||||||
|
->label('Jenis Kelamin')
|
||||||
|
->badge()
|
||||||
|
->sortable()
|
||||||
|
->searchable(query: function ($query, string $search) {
|
||||||
|
$search = strtolower($search);
|
||||||
|
|
||||||
|
if (str_contains('laki-laki', $search)) {
|
||||||
|
$query->orWhere('sex', 'male');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (str_contains('perempuan', $search)) {
|
||||||
|
$query->orWhere('sex', 'female');
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
|
TextColumn::make('date_of_birth')
|
||||||
|
->label('TTL')
|
||||||
|
->date('d F Y')
|
||||||
|
->sortable()
|
||||||
|
->prefix(fn (Lecturer $record) => $record->place_of_birth ? $record->place_of_birth.', ' : ''),
|
||||||
|
|
||||||
|
TextColumn::make('address')
|
||||||
|
->label('Alamat')
|
||||||
|
->limit(50),
|
||||||
|
|
||||||
|
...TimestampColumns::make(),
|
||||||
|
])
|
||||||
|
->filters([
|
||||||
|
SelectFilter::make('sex')
|
||||||
|
->label('Jenis Kelamin')
|
||||||
|
->options(Sex::class)
|
||||||
|
->native(false),
|
||||||
|
|
||||||
|
TrashedFilter::make()
|
||||||
|
->native(false),
|
||||||
|
])
|
||||||
|
->recordActions([
|
||||||
|
EditAction::make()
|
||||||
|
->modalWidth(Width::Large),
|
||||||
|
|
||||||
|
DeleteAction::make(),
|
||||||
|
|
||||||
|
ForceDeleteAction::make(),
|
||||||
|
|
||||||
|
RestoreAction::make(),
|
||||||
|
])
|
||||||
|
->toolbarActions([
|
||||||
|
BulkActionGroup::make([
|
||||||
|
...DefaultBulkActions::make('Dosen'),
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->emptyStateIcon(Heroicon::OutlinedUserGroup)
|
||||||
|
->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
|
||||||
|
->defaultSort('created_at', 'desc')
|
||||||
|
->deferFilters(false)
|
||||||
|
->paginated([25, 50, 100, 'all'])
|
||||||
|
->defaultPaginationPageOption(25);
|
||||||
|
}
|
||||||
|
}
|
||||||
43
app/Models/Lecturer.php
Normal file
43
app/Models/Lecturer.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\Enums\Sex;
|
||||||
|
use Illuminate\Database\Eloquent\Attributes\Scope;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class Lecturer extends Model
|
||||||
|
{
|
||||||
|
use HasFactory, SoftDeletes;
|
||||||
|
|
||||||
|
protected $guarded = ['id'];
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'date_of_birth' => 'date',
|
||||||
|
'sex' => Sex::class,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Scope]
|
||||||
|
protected function female(Builder $query): void
|
||||||
|
{
|
||||||
|
$query->where('sex', Sex::Female);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[Scope]
|
||||||
|
protected function male(Builder $query): void
|
||||||
|
{
|
||||||
|
$query->where('sex', Sex::Male);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function user(): BelongsTo
|
||||||
|
{
|
||||||
|
return $this->belongsTo(User::class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Enums\Sex;
|
||||||
|
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('lecturers', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('user_id')->unique()->constrained()->cascadeOnDelete();
|
||||||
|
$table->string('lecturer_identification_number', 10)->unique();
|
||||||
|
$table->string('full_name', 100);
|
||||||
|
$table->string('phone_number', 20);
|
||||||
|
$table->enum('sex', Sex::cases());
|
||||||
|
$table->text('address')->nullable();
|
||||||
|
$table->date('date_of_birth');
|
||||||
|
$table->string('place_of_birth', 50);
|
||||||
|
$table->timestamp('created_at')->useCurrent();
|
||||||
|
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
|
||||||
|
$table->softDeletes();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('lecturers');
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user