PHP code example of comhon-project / laravel-morphed-model-exporter
1. Go to this page and download the library: Download comhon-project/laravel-morphed-model-exporter library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
comhon-project / laravel-morphed-model-exporter example snippets
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,
],
]
}
}
public function register(): void
{
$this->app->bind('morphed-model-exporters', MyMorphedModelsExporters::class);
}
use Comhon\MorphedModelExporter\Facades\MorphedModelExporter;
MorphedModelExporter::loadMorphedModels($myModels, 'myMorphToRelation');
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']);
use Comhon\MorphedModelExporter\Facades\MorphedModelExporter;
'my_morph_to_relation' => $this->whenLoaded(
'myMorphToRelation',
fn ($morphedModel) => MorphedModelExporter::exportModel($morphedModel)
),
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);