Of course. Here is the complete, final solution for the template-basedDocumentation Index
Fetch the complete documentation index at: https://docs.mejik.web.id/llms.txt
Use this file to discover all available pages before exploring further.
export:header-detail command, including all helper classes and the full documentation without any summarized code blocks.
1. Prerequisites
Before using the command, ensure your Laravel project is set up correctly.1.1. Required Packages
Install the necessary package via Composer:PhpSpreadsheet is automatically included with this package.
1.2. Example Model Setup
For the export command to work, your parent model must define the relationships to its children.app/Models/JournalHeader.php
app/Models/JournalDetail.php
2. Full Code
2.1. Helper Export Classes
These classes are used by the main command to manage sheets. Create a new directoryapp/Exports.
File: app/Exports/SingleSheetExport.php
app/Exports/MultiSheetHeaderDetailExport.php
2.2. Main Command File
This is the complete and final code for the template-based export command. File:app/Console/Commands/ExportHeaderDetail.php
populateSheetWithData logic for inserting rows to ensure it works correctly for single and multiple data rows.
3. Usage Documentation
English Documentation
Command Synopsis
Key Argument: --template
This command requires a path to an Excel file that will serve as the template for the output. The command will load this file, inject data into it, and save a new file.
Template File Structure
The template file must contain “placeholder rows” that tell the command where to insert data.- Column A: Must contain an entity type keyword (e.g.,
header,detail). This keyword must match a key provided in your--entitiesargument. - Columns B onwards: On the same row as the keyword, you must list the exact technical field names from your database model (e.g.,
journalId,totalAmount). The command reads these names to map your data to the correct columns. - All other rows (titles, styled headers, blank rows) and cell formatting (colors, fonts, borders) will be preserved from the template.
storage/app/templates/journal_template.xlsx):
| A | B | C | D | E | ||
|---|---|---|---|---|---|---|
| 1 | Journal Head | |||||
| 2 | Journal ID | Description | Date | Total | ||
| 3 | header | journalId | journalDescription | journalDate | totalAmount | |
| 4 | ||||||
| 5 | Journal Detail | |||||
| 6 | Journal ID | Description | Date | Qty | … | |
| 7 | detail | journalId | trxDescription | trxDate | trxQty | … |
Example Usage
To export two specificJournalHeader records, each on its own sheet, using the template:
- Command:
- Result: A file named
MyExport.xlsxis created with two sheets: “J001” and “J002”. Each sheet perfectly matches the template’s styling and structure, but is populated with the data for the respective journal. The original placeholder rows are replaced by the data rows.
Dokumentasi Bahasa Indonesia
Sintaks Perintah
Argumen Kunci: --template
Perintah ini mewajibkan path ke sebuah file Excel yang akan berfungsi sebagai template untuk output. Perintah akan memuat file ini, memasukkan data ke dalamnya, dan menyimpan file baru.
Struktur File Template
File template harus berisi “baris placeholder” untuk memberitahu perintah di mana harus memasukkan data.- Kolom A: Harus berisi kata kunci tipe entitas (cth:
header,detail). Kata kunci ini harus cocok dengan kunci yang diberikan di argumen--entities. - Kolom B dan seterusnya: Pada baris yang sama dengan kata kunci, Anda harus mendaftarkan nama-nama kolom teknis yang persis sama dengan di model database Anda (cth:
journalId,totalAmount). Perintah akan membaca nama-nama ini untuk memetakan data ke kolom yang benar. - Semua baris lain (judul, header dengan style, baris kosong) dan format sel (warna, font, border) akan dipertahankan persis seperti di template.
storage/app/templates/journal_template.xlsx):
| A | B | C | D | E | ||
|---|---|---|---|---|---|---|
| 1 | Journal Head | |||||
| 2 | Journal ID | Description | Date | Total | ||
| 3 | header | journalId | journalDescription | journalDate | totalAmount | |
| 4 | ||||||
| 5 | Journal Detail | |||||
| 6 | Journal ID | Description | Date | Qty | … | |
| 7 | detail | journalId | trxDescription | trxDate | trxQty | … |
Contoh Penggunaan
Mengekspor dua recordJournalHeader spesifik, masing-masing ke sheet terpisah, menggunakan template:
- Perintah:
- Hasil: Sebuah file bernama
HasilEkspor.xlsxakan dibuat dengan dua sheet: “J001” dan “J002”. Setiap sheet akan memiliki style dan struktur yang sama persis dengan template, namun sudah diisi dengan data untuk jurnal yang bersangkutan. Baris placeholder asli akan digantikan oleh baris-baris data.
