Skip to main content

1. Prerequisites

Before using the commands, ensure your Laravel project is set up correctly.

1.1. Required Packages

Install the necessary packages via Composer:

1.2. Example SQL Setup (for Relational Data)

Migrations:
  • ..._create_journal_headers_table.php
  • ..._create_journal_details_table.php
Models:
  • app/Models/JournalHeader.php
  • app/Models/JournalDetail.php

1.3. Example NoSQL Setup (for Embedded Data)

Model:
  • app/Models/PersonnelInfo.php

2. Full Code: import:header-detail Command

File: app/Console/Commands/ImportHeaderDetail.php

3. Full Code: export:header-detail Command

3.1. Helper Export Classes

Create a new directory app/Exports. File: app/Exports/SingleSheetExport.php
File: app/Exports/MultiSheetHeaderDetailExport.php

3.2. Main Command File

File: app/Console/Commands/ExportHeaderDetail.php

4. Comprehensive Usage Documentation

English Documentation

import:header-detail
  • Synopsis: php artisan import:header-detail <file> [options]
  • Arguments & Options: | Parameter | Description | |---|---| | file | Path to the Excel/CSV file to import. | | --entities | Required. Map types to Model names (e.g., header:JournalHeader,detail:JournalDetail). | | --key | Required. The name of the primary/foreign key column (e.g., journalId). | | --embed | For NoSQL, embed child entities as an array in the parent document. | | --sheet-title-as| For Excel, use each sheet’s title as a value for the specified field. |
export:header-detail
  • Synopsis: php artisan export:header-detail <file> [options]
  • Arguments & Options: | Parameter | Description | |---|---| | file | The output file path (e.g., storage/app/export.xlsx). | | --entities | Required. Map types to Models (for header) and relationship/array names (for details). Ex: header:JournalHeader,detail:details. | | --key | Required. The primary key field of the parent model. | | --query | Filter records with a query string (e.g., "status:active;amount:>:5000"). | | --ids | A comma-separated list of parent primary keys to export (e.g., J001,J002). | | --embed | Export from a NoSQL model with embedded arrays. | | --sheet-title-as| Export each parent record to its own sheet, named by this field’s value. |

Dokumentasi Bahasa Indonesia

import:header-detail
  • Sintaks: php artisan import:header-detail <file> [options]
  • Argumen & Opsi: | Parameter | Deskripsi | |---|---| | file | Path menuju file Excel/CSV yang akan diimpor. | | --entities | Wajib. Memetakan tipe ke nama Model (cth: header:JournalHeader,detail:JournalDetail). | | --key | Wajib. Nama kolom kunci primer/asing (primary/foreign key) (cth: journalId). | | --embed | Untuk NoSQL, sematkan data turunan (child) sebagai array di dalam dokumen induk. | | --sheet-title-as| Untuk Excel, gunakan judul setiap sheet sebagai nilai untuk kolom yang ditentukan. |
export:header-detail
  • Sintaks: php artisan export:header-detail <file> [options]
  • Argumen & Opsi: | Parameter | Deskripsi | |---|---| | file | Path file output untuk menyimpan hasil ekspor (cth: storage/app/ekspor.xlsx). | | --entities | Wajib. Memetakan tipe ke Model (untuk header) dan nama relasi/array (untuk detail). Cth: header:JournalHeader,detail:details. | | --key | Wajib. Nama kolom kunci primer dari model induk. | | --query | Filter data menggunakan string kueri (cth: "status:active;amount:>:5000"). | | --ids | Daftar kunci primer data induk yang akan diekspor, dipisahkan koma (cth: J001,J002). | | --embed | Ekspor dari model NoSQL dengan data detail yang tersemat (embedded). | | --sheet-title-as| Ekspor setiap data induk ke sheet-nya sendiri, dinamai berdasarkan nilai dari kolom ini. |