feat: Create Student resource with CRUD pages and form schema for managing student data in the admin panel.
This commit is contained in:
parent
11a5740409
commit
5e63ef2744
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Master\Students\Pages;
|
||||
|
||||
use App\Filament\Actions\BackAction;
|
||||
use App\Filament\Resources\Master\Students\StudentResource;
|
||||
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 CreateStudent extends CreateRecord
|
||||
{
|
||||
protected static string $resource = StudentResource::class;
|
||||
|
||||
protected ?string $heading = 'Tambah Mahasiswa';
|
||||
|
||||
protected static ?string $title = 'Tambah Mahasiswa';
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
BackAction::make()
|
||||
->url(ListStudents::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']);
|
||||
|
||||
$student = $this->getResource()::getModel()::create(
|
||||
array_merge($data, [
|
||||
'user_id' => $user->id,
|
||||
])
|
||||
);
|
||||
|
||||
return $student;
|
||||
});
|
||||
}
|
||||
}
|
||||
36
app/Filament/Resources/Master/Students/Pages/EditStudent.php
Normal file
36
app/Filament/Resources/Master/Students/Pages/EditStudent.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Master\Students\Pages;
|
||||
|
||||
use App\Filament\Actions\BackAction;
|
||||
use App\Filament\Resources\Master\Students\StudentResource;
|
||||
use App\Filament\Support\SystemNotification;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditStudent extends EditRecord
|
||||
{
|
||||
protected static string $resource = StudentResource::class;
|
||||
|
||||
protected ?string $heading = 'Ubah Mahasiswa';
|
||||
|
||||
protected static ?string $title = 'Ubah Mahasiswa';
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
BackAction::make()
|
||||
->url(ListStudents::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\Students\Pages;
|
||||
|
||||
use App\Filament\Actions\Cheerful\CreateAction;
|
||||
use App\Filament\Resources\Master\Students\StudentResource;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListStudents extends ListRecords
|
||||
{
|
||||
protected static string $resource = StudentResource::class;
|
||||
|
||||
protected static ?string $title = 'Mahasiswa';
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make()
|
||||
->label('Tambah'),
|
||||
];
|
||||
}
|
||||
}
|
||||
116
app/Filament/Resources/Master/Students/Schemas/StudentForm.php
Normal file
116
app/Filament/Resources/Master/Students/Schemas/StudentForm.php
Normal file
@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Master\Students\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 StudentForm
|
||||
{
|
||||
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 Mahasiswa')
|
||||
->schema([
|
||||
TextInput::make('full_name')
|
||||
->label('Nama Lengkap')
|
||||
->placeholder('John Doe')
|
||||
->autocomplete(false)
|
||||
->required()
|
||||
->maxLength(100)
|
||||
->minLength(3)
|
||||
->autofocus()
|
||||
->columnSpanFull(),
|
||||
|
||||
Grid::make(3)
|
||||
->schema([
|
||||
TextInput::make('student_number')
|
||||
->label('NIM')
|
||||
->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(),
|
||||
|
||||
DatePicker::make('date_of_birth')
|
||||
->label('Tanggal Lahir')
|
||||
->placeholder('Pilih Tanggal')
|
||||
->required()
|
||||
->native(false)
|
||||
->displayFormat('l, d F Y')
|
||||
->maxDate(Carbon::now()->subYears(15)),
|
||||
|
||||
TextInput::make('place_of_birth')
|
||||
->label('Tempat Lahir')
|
||||
->placeholder('Purwakarta')
|
||||
->autocomplete(false)
|
||||
->required()
|
||||
->maxLength(50)
|
||||
->minLength(3),
|
||||
])
|
||||
->columns(2),
|
||||
])
|
||||
->columns(1);
|
||||
}
|
||||
}
|
||||
52
app/Filament/Resources/Master/Students/StudentResource.php
Normal file
52
app/Filament/Resources/Master/Students/StudentResource.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Master\Students;
|
||||
|
||||
use App\Filament\Resources\Master\Students\Pages\CreateStudent;
|
||||
use App\Filament\Resources\Master\Students\Pages\EditStudent;
|
||||
use App\Filament\Resources\Master\Students\Pages\ListStudents;
|
||||
use App\Filament\Resources\Master\Students\Schemas\StudentForm;
|
||||
use App\Filament\Resources\Master\Students\Tables\StudentsTable;
|
||||
use App\Models\Student;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
use UnitEnum;
|
||||
|
||||
class StudentResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Student::class;
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Master';
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedUserPlus;
|
||||
|
||||
protected static ?string $navigationLabel = 'Mahasiswa';
|
||||
|
||||
protected static ?int $navigationSort = 1;
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'full_name';
|
||||
|
||||
protected static ?string $slug = 'master/students';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return StudentForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return StudentsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListStudents::route('/'),
|
||||
'create' => CreateStudent::route('/create'),
|
||||
'edit' => EditStudent::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
113
app/Filament/Resources/Master/Students/Tables/StudentsTable.php
Normal file
113
app/Filament/Resources/Master/Students/Tables/StudentsTable.php
Normal file
@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Master\Students\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\Student;
|
||||
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 StudentsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('user.email')
|
||||
->label('Akun')
|
||||
->formatStateUsing(fn ($state, Student $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('student_number', 'like', "%{$search}%");
|
||||
})
|
||||
->sortable()
|
||||
->description(fn (Student $student) => $student->student_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 (Student $student) => $student->place_of_birth ? $student->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('Mahasiswa'),
|
||||
]),
|
||||
])
|
||||
->emptyStateIcon(Heroicon::OutlinedUserPlus)
|
||||
->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
|
||||
->defaultSort('created_at', 'desc')
|
||||
->deferFilters(false)
|
||||
->paginated([25, 50, 100, 'all'])
|
||||
->defaultPaginationPageOption(25);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user