comhon-project / laravel-morphed-model-exporter
A laravel library that permit to export morphed models through an API
Requires
- php: ^8.2
- illuminate/contracts: ^10.0||^11.0||^12.0
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0|^10.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- phpunit/phpunit: ^10.5|^11.0|^12.0
- spatie/laravel-ray: ^1.35
README
This library allows exporting morphed models (typically via an API). A morphed model is one that is loaded through a MorphTo
relationship. Since these models belong to different classes, loading them from a collection along with their dependencies and exporting them can be cumbersome. This library makes it easy!
Installation
You can install the package via composer:
composer require comhon-project/laravel-morphed-model-exporter
Usage
Register exporters
In order to be able to export morphed models, you must define morphed model exporters.
To do so, you must define a class with an __invoke()
method that will return an array of exporters. Each key must be a eloquent model class and each value must be an array. Each array value must/may contain:
- the key
model_exporter
(required). The associated value must be either a Closure and return the exported model (the eloquent model is inject as parameter) either a JsonResource class. - the key
query_builder
(optional). The associated value must be a Closure that will build the query given in parameter.
class MyMorphedModelsExporters { public function __construct(private array $exporters) {} public function __invoke() { return [ FirstModel::class => [ 'query_builder' => fn ($query) => $query->with('dependency')->select('id', 'dependency_id'), 'model_exporter' => fn ($model) => $model->toArray(), ], SecondModel::class => [ 'model_exporter' => SecondModel::class, ], ] } }
Then you will have to register it in your AppServiceProvider
like this :
public function register(): void { $this->app->bind('morphed-model-exporters', MyMorphedModelsExporters::class); }
Load morphed models
You should typically load morphed models in Controllers :
use Comhon\MorphedModelExporter\Facades\MorphedModelExporter; MorphedModelExporter::loadMorphedModels($myModels, 'myMorphToRelation');
You can use additional parameters to load differents data according a certain context :
class MyMorphedModelsExporters { public function __construct(private array $exporters) {} public function __invoke() { return [ FirstModel::class => [ 'query_builder' => fn ($query, array $additionalColumns = []) => $query->select([ 'id', 'dependency_id', ...$additionalColumns ]), ], ] } }
use Comhon\MorphedModelExporter\Facades\MorphedModelExporter; MorphedModelExporter::loadMorphedModels($myModels, 'myMorphToRelation', ['my_column']);
Export morphed models
You should typically export morphed models in API resources :
use Comhon\MorphedModelExporter\Facades\MorphedModelExporter; 'my_morph_to_relation' => $this->whenLoaded( 'myMorphToRelation', fn ($morphedModel) => MorphedModelExporter::exportModel($morphedModel) ),
You can use additional parameters to export differents properties according a certain context :
class MyMorphedModelsExporters { public function __construct(private array $exporters) {} public function __invoke() { return [ FirstModel::class => [ 'model_exporter' => fn ($model, $private = false) => $private ? ['id' => $model->id, 'private' => $model->private] : ['id' => $model->id], ], ] } }
use Comhon\MorphedModelExporter\Facades\MorphedModelExporter; $private = true; MorphedModelExporter::exportModel($morphedModel, $private);
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.