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β
Feature | Description |
---|---|
Auto-export columns | All DB columns exported by default |
Custom export fields | Use getExportableColumns() in model |
Localization support | Automatically handles translatable fields |
Plug & play setup | Just define route + controller + trait usage in model |
Dynamic headers | Use Model::exportableDataColumnNames() |