Skip to main content
This is an excellent evolution of the command, making it significantly more versatile for both SQL (relational) and NoSQL (document-based) databases, as well as more powerful for handling complex Excel files. Here is the complete, updated solution that incorporates both new features.

1. Prerequisites (MongoDB Setup - Optional)

If you plan to use the --embed feature with MongoDB, you must install and configure a MongoDB driver package for Laravel. The most common one is jenssegers/laravel-mongodb.
  1. Install the package:
  1. Configure config/database.php: Add a new connection for MongoDB.
  2. Update your Models: The parent model (e.g., JournalHeader) should extend Jenssegers\Mongodb\Eloquent\Model instead of the default Eloquent model.
Example MongoDB Model (app/Models/JournalHeader.php):
Child models (JournalDetail) are not strictly required for embedding, as they will just become arrays of objects within the parent document.

2. Complete Artisan Command Code

Replace the entire content of app/Console/Commands/ImportHeaderDetail.php with the following code. It has been significantly refactored to handle the new logic.

3. Usage Documentation (English)

Command Synopsis

Arguments & Options

Use Case 1: Standard Relational Import (SQL)

This is the default behavior, same as before. Child entities are saved to their own tables with a foreign key.

Use Case 2: Embedded Document Import (MongoDB)

This mode is activated with the --embed flag. It’s ideal for document databases. Example Excel File: Same as the HR example file from the previous response. Command:
Resulting MongoDB Document in personnel_infos collection:

Use Case 3: Multi-Sheet Excel Import

This powerful mode is for Excel files where each sheet represents a complete header-detail record. The sheet title itself becomes a key piece of data. Example Excel File Structure: Imagine a file named journals_by_id.xlsx with two sheets named J001 and J002. Sheet “J001”: Sheet “J002”: Command:
How it works:
  • It processes sheet “J001” first. For every row, it automatically adds journalId => 'J001'.
  • Then it processes sheet “J002”. For every row on this sheet, it adds journalId => 'J002'.
  • This creates two distinct JournalHeader records with their respective JournalDetail records, all correctly linked. This can also be combined with --embed.

4. Dokumentasi Penggunaan (Bahasa Indonesia)

Sintaks Perintah

Argumen & Opsi

Kasus Penggunaan 1: Impor Relasional Standar (SQL)

Ini adalah mode default, sama seperti sebelumnya. Entitas turunan disimpan ke tabelnya masing-masing dengan sebuah foreign key.

Kasus Penggunaan 2: Impor Dokumen Tersemat (MongoDB)

Mode ini diaktifkan dengan flag --embed. Sangat ideal untuk database berbasis dokumen. Contoh Perintah:
Hasil Dokumen MongoDB di collection personnel_infos:

Kasus Penggunaan 3: Impor Excel Multi-Sheet

Mode ini digunakan untuk file Excel di mana setiap sheet mewakili satu set record header-detail yang lengkap. Judul sheet itu sendiri menjadi bagian penting dari data. Struktur File Excel jurnal_per_id.xlsx dengan dua sheet bernama J001 dan J002. Sheet “J001”: Sheet “J002”: Perintah:
Cara Kerja:
  • Perintah akan memproses sheet “J001” terlebih dahulu. Untuk setiap baris, ia otomatis menambahkan data journalId => 'J001'.
  • Kemudian, ia memproses sheet “J002” dan menambahkan journalId => 'J002' untuk setiap baris di sheet tersebut.
  • Hasilnya adalah dua record JournalHeader yang berbeda dengan JournalDetail yang terhubung dengan benar. Mode ini juga bisa dikombinasikan dengan --embed.