Skip to main content

1. Prerequisites: Models & Migrations

For this command to work, you need the corresponding Eloquent models and database tables. Here is the example setup for the JournalHeader and JournalDetail scenario.

Migrations

..._create_journal_headers_table.php
..._create_journal_details_table.php
Run php artisan migrate after creating these.

Models

app/Models/JournalHeader.php
app/Models/JournalDetail.php

2. Complete Artisan Command Code

Create the command with php 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 journalId specified. It will be automatically linked to the last processed header, which is J001.
  • Row 6 has journalId explicitly 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:
How this is processed:
  1. A PersonnelInfo record is created for John Doe (E001).
  2. The two subsequent dependent rows are processed, creating Dependent records that are automatically linked to E001.
  3. The final education row is processed, creating an EducationHistory record that is also automatically linked to E001.

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 journalId yang 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:
Bagaimana ini diproses:
  1. Sebuah record PersonnelInfo dibuat untuk John Doe (E001).
  2. Dua baris dependent berikutnya diproses, membuat record Dependent yang secara otomatis terhubung ke E001.
  3. Baris education terakhir diproses, membuat record EducationHistory yang juga terhubung secara otomatis ke E001.

Contoh Eksekusi Perintah (Jurnal)