1. Prerequisites: Models & Migrations
For this command to work, you need the corresponding Eloquent models and database tables. Here is the example setup for theJournalHeader and JournalDetail scenario.
Migrations
..._create_journal_headers_table.php
..._create_journal_details_table.php
php artisan migrate after creating these.
Models
app/Models/JournalHeader.php
app/Models/JournalDetail.php
2. Complete Artisan Command Code
Create the command withphp artisan make:command ImportHeaderDetail. Place the following code into the generated file at app/Console/Commands/ImportHeaderDetail.php.
3. Usage Documentation (English)
Command Synopsis
Arguments & Options
Example Excel File Structure
The importer relies on a specific structure. Column A indicates the type of data block. The row immediately following the type declaration must contain the column headers, which must match the$fillable fields in your model.
How this file is processed:
- Row 5 does not have
journalIdspecified. It will be automatically linked to the last processed header, which is J001. - Row 6 has
journalIdexplicitly set to J001. The importer will use this value.
Advanced Example: Multiple Detail Types (HR Data)
The command is powerful enough to handle a main record with multiple, different types of child records. For example, a single employee can have both dependents and an education history. Example HR Excel File:
Command to Import HR Data:
Assuming you have the models
PersonnelInfo, Dependent, and EducationHistory:
- A
PersonnelInforecord is created for John Doe (E001). - The two subsequent
dependentrows are processed, creatingDependentrecords that are automatically linked toE001. - The final
educationrow is processed, creating anEducationHistoryrecord that is also automatically linked toE001.
Example Command Execution (Journal)
4. Dokumentasi Penggunaan (Bahasa Indonesia)
Sintaks Perintah
Argumen & Opsi
Contoh Struktur File Excel
Importer ini bergantung pada struktur file yang spesifik. Kolom A menandakan tipe blok data. Baris setelah deklarasi tipe harus berisi nama-nama kolom (header), yang harus cocok dengan properti$fillable pada Model Anda.
Bagaimana file ini diproses:
- Baris 5 tidak memiliki nilai pada kolom
journalId. Ia akan otomatis terhubung ke header terakhir yang diproses, yaitu J001. - Baris 6 memiliki nilai
journalIdyang diisi secara eksplisit. Importer akan menggunakan nilai ini.
Contoh Lanjutan: Beberapa Tipe Detail (Data HR)
Perintah ini cukup andal untuk menangani satu data induk yang memiliki beberapa jenis data turunan yang berbeda. Sebagai contoh, satu karyawan dapat memiliki data tanggungan (dependent) dan juga riwayat pendidikan (education history). Contoh File Excel HR:
Perintah untuk Impor Data HR:
Dengan asumsi Anda memiliki model
PersonnelInfo, Dependent, dan EducationHistory:
- Sebuah record
PersonnelInfodibuat untuk John Doe (E001). - Dua baris
dependentberikutnya diproses, membuat recordDependentyang secara otomatis terhubung keE001. - Baris
educationterakhir diproses, membuat recordEducationHistoryyang juga terhubung secara otomatis keE001.
