Skip to main content

Export

Here's a structured and developer-friendly documentation page section for the Export feature of your Laravel Admin Starter Kit Peak:

πŸ“€ Export (Development Guide)​

Overview​

Peak provides a powerful and flexible export system out of the box, enabling you to quickly export model data like users, orders, etc., to formats such as CSV or Excel. It is designed to be developer-friendly, extendable, and highly customizable.

At its core, the export system works through:

  • A dedicated controller per exportable resource.
  • A common base export controller.
  • A model trait: HasExportableData, which dynamically defines exportable columns.
  • Optional customization via getExportableColumns() on the model.

🧭 Basic Usage​

1. Define an Export Route​

For example, to export users, define the route like this:

Route::get('exports/users', UserExportController::class)->name('export.users');

This maps the request to the UserExportController.

2. Create an Export Controller​

Create a controller that extends Peak’s base ExportController and define the model and table name.

namespace Qoraiche\Peak\Http\Controllers\Admin\Exports;

use Qoraiche\Peak\Models\User;

class UserExportController extends ExportController
{
public string $classModel = User::class;
public string $tableName = 'users';
}

βœ… ExportController handles the core logic like resolving exportable fields and generating the response (CSV, Excel, etc.).

🧩 Making Models Exportable​

To make any model exportable, you must use the HasExportableData trait:

use Qoraiche\Peak\Traits\HasExportableData;

class User extends Model
{
use HasExportableData;
}

βš™οΈ How It Works​

The export system automatically detects columns to be exported by using Laravel's Schema to list all table columns. You can override and customize this behavior.

Default Behavior​

If you don’t define a getExportableColumns() method in your model, the system will automatically export all table columns. If a column is translatable, its value is translated based on the current locale.

Trait Logic (Simplified):

public function getExportableColumns(): array
{
$columns = Schema::getColumnListing($this->getTable());

return collect($columns)->mapWithKeys(fn($column) => [
$column => fn($model) => $model->$column
])->toArray();
}

πŸ“ Customizing Exported Columns​

If you want to export only specific columns or apply transformation logic (e.g., format names, dates, etc.), override the getExportableColumns() method in your model:

public function getExportableColumns(): array
{
return [
'id' => fn($user) => $user->id,
'name' => fn($user) => strtoupper($user->name), // uppercase name
'email' => fn($user) => $user->email,
];
}

βœ… This gives you fine-grained control over the exported data.

πŸ“₯ Static Export Column Names​

Sometimes, you might need the column names before exporting the data (e.g., to build headers). The trait provides a static helper:

User::exportableDataColumnNames();
// Returns: ['id', 'name', 'email']

🌐 Localization Support​

If your model has translatable fields (e.g., using spatie/laravel-translatable), and defines a $translatable property, the export system will automatically export values in the current locale.

public array $translatable = ['name'];

In that case:

'name' => fn($model) => $model->getTranslation('name', Helpers::getLocale())

πŸ”š Summary​

FeatureDescription
Auto-export columnsAll DB columns exported by default
Custom export fieldsUse getExportableColumns() in model
Localization supportAutomatically handles translatable fields
Plug & play setupJust define route + controller + trait usage in model
Dynamic headersUse Model::exportableDataColumnNames()