add import student

This commit is contained in:
reihanrere 2025-05-25 17:09:31 +07:00
parent f057b0c283
commit b98c57d565
6 changed files with 215 additions and 0 deletions

View File

@ -0,0 +1,81 @@
<?php
namespace App\Filament\Imports;
use App\Models\Student;
use App\Models\User;
use Filament\Actions\Imports\ImportColumn;
use Filament\Actions\Imports\Importer;
use Filament\Actions\Imports\Models\Import;
use Filament\Actions\Imports\Exceptions\RowImportFailedException;
class StudentImporter extends Importer
{
protected static ?string $model = Student::class;
public static function getColumns(): array
{
return [
ImportColumn::make('full_name')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('nis')
->rules(['required','max:255']),
ImportColumn::make('nisn')
->rules(['required','max:255']),
ImportColumn::make('gender')
->requiredMapping()
->rules(['required', 'max:255']),
ImportColumn::make('birth_date')
->rules(['date']),
ImportColumn::make('birth_place')
->rules(['max:255']),
ImportColumn::make('address'),
ImportColumn::make('religion'),
ImportColumn::make('phone')
->rules(['required','max:255']),
ImportColumn::make('email')
->rules(['email', 'max:255']),
ImportColumn::make('parent_name')
->rules(['required','max:255']),
ImportColumn::make('parent_phone')
->rules(['required','max:255']),
];
}
public function resolveRecord(): ?Student
{
// Cek duplikat NIS
if (Student::where('nis', $this->data['nis'])->exists()) {
throw new RowImportFailedException('NIS sudah terdaftar');
}
// Cek duplikat NISN
if (Student::where('nisn', $this->data['nisn'])->exists()) {
throw new RowImportFailedException('NISN sudah terdaftar');
}
// Cek duplikat phone
if (Student::where('phone', $this->data['phone'])->exists()) {
throw new RowImportFailedException('Nomor telepon sudah terdaftar');
}
// Cek duplikat email di tabel users
if (User::where('email', $this->data['email'])->exists()) {
throw new RowImportFailedException('Email sudah digunakan');
}
return new Student();
}
public static function getCompletedNotificationBody(Import $import): string
{
$body = 'Your student import has completed and ' . number_format($import->successful_rows) . ' ' . str('row')->plural($import->successful_rows) . ' imported.';
if ($failedRowsCount = $import->getFailedRowsCount()) {
$body .= ' ' . number_format($failedRowsCount) . ' ' . str('row')->plural($failedRowsCount) . ' failed to import.';
}
return $body;
}
}

View File

@ -2,6 +2,7 @@
namespace App\Filament\Resources\StudentResource\Pages; namespace App\Filament\Resources\StudentResource\Pages;
use App\Filament\Imports\StudentImporter;
use App\Filament\Resources\StudentResource; use App\Filament\Resources\StudentResource;
use Filament\Actions; use Filament\Actions;
use Filament\Resources\Pages\ListRecords; use Filament\Resources\Pages\ListRecords;
@ -14,6 +15,8 @@ class ListStudents extends ListRecords
{ {
return [ return [
Actions\CreateAction::make(), Actions\CreateAction::make(),
Actions\ImportAction::make('importBrands')
->importer(StudentImporter::class),
]; ];
} }
} }

View File

@ -0,0 +1,31 @@
<?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::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('notifications');
}
};

View File

@ -0,0 +1,35 @@
<?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::create('imports', function (Blueprint $table) {
$table->id();
$table->timestamp('completed_at')->nullable();
$table->string('file_name');
$table->string('file_path');
$table->string('importer');
$table->unsignedInteger('processed_rows')->default(0);
$table->unsignedInteger('total_rows');
$table->unsignedInteger('successful_rows')->default(0);
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('imports');
}
};

View File

@ -0,0 +1,35 @@
<?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::create('exports', function (Blueprint $table) {
$table->id();
$table->timestamp('completed_at')->nullable();
$table->string('file_disk');
$table->string('file_name')->nullable();
$table->string('exporter');
$table->unsignedInteger('processed_rows')->default(0);
$table->unsignedInteger('total_rows');
$table->unsignedInteger('successful_rows')->default(0);
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('exports');
}
};

View File

@ -0,0 +1,30 @@
<?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::create('failed_import_rows', function (Blueprint $table) {
$table->id();
$table->json('data');
$table->foreignId('import_id')->constrained()->cascadeOnDelete();
$table->text('validation_error')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('failed_import_rows');
}
};