PHPackages                             comhon-project/laravel-morphed-model-exporter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. comhon-project/laravel-morphed-model-exporter

ActiveLibrary[API Development](/categories/api)

comhon-project/laravel-morphed-model-exporter
=============================================

A laravel library that permit to export morphed models through an API

v1.2.0(8mo ago)0152[3 PRs](https://github.com/comhon-project/laravel-morphed-model-exporter/pulls)MITPHPPHP ^8.2CI passing

Since May 15Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/comhon-project/laravel-morphed-model-exporter)[ Packagist](https://packagist.org/packages/comhon-project/laravel-morphed-model-exporter)[ Docs](https://github.com/comhon-project/laravel-morphed-model-exporter)[ RSS](/packages/comhon-project-laravel-morphed-model-exporter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (10)Versions (10)Used By (0)

Laravel Morphed Model Exporter
==============================

[](#laravel-morphed-model-exporter)

[![Latest Version on Packagist](https://camo.githubusercontent.com/787dbb9ef3fbe7e43585458e73b84f80799f0740375612c65c9a6d0ae01927ec/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f6d686f6e2d70726f6a6563742f6c61726176656c2d6d6f72706865642d6d6f64656c2d6578706f727465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/comhon-project/laravel-morphed-model-exporter)[![GitHub Tests Action Status](https://camo.githubusercontent.com/ac1512a747ff59ff4461d62512764b32db5b1d227a0bf7447bafa907ef7dfc32/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636f6d686f6e2d70726f6a6563742f6c61726176656c2d6d6f72706865642d6d6f64656c2d6578706f727465722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/comhon-project/laravel-morphed-model-exporter/actions?query=workflow%3Arun-tests+branch%3Amain)[![Code Coverage](https://camo.githubusercontent.com/777f32d1026dd8f7a57f2c3cb10ea1da08c2ca45300209b65a98e1b0d93718b5/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f636f6d686f6e2d70726f6a6563742f6c61726176656c2d6d6f72706865642d6d6f64656c2d6578706f727465723f6c6162656c3d636f766572616765267374796c653d666c61742d737175617265)](https://codecov.io/gh/comhon-project/laravel-morphed-model-exporter)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/b2efd744cfbd620b89722557e66bf53b207d195ba2e95845facceeff532532c7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636f6d686f6e2d70726f6a6563742f6c61726176656c2d6d6f72706865642d6d6f64656c2d6578706f727465722f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/comhon-project/laravel-morphed-model-exporter/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/ad76d95141c7c223f6b5e475a0b01a6ecf3fef36d29212e444cf4e9b34198a87/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f6d686f6e2d70726f6a6563742f6c61726176656c2d6d6f72706865642d6d6f64656c2d6578706f727465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/comhon-project/laravel-morphed-model-exporter)

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
------------

[](#installation)

You can install the package via composer:

```
composer require comhon-project/laravel-morphed-model-exporter
```

Usage
-----

[](#usage)

### Register exporters

[](#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 __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);
}
```

Note: Your morphed model exporters class may also type-hint any dependencies they need on their constructors. This class is resolved via the Laravel service container, so dependencies will be injected automatically.

### Load morphed models

[](#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 customize query according a certain context :

```
class MyMorphedModelsExporters
{
    public function __invoke()
    {
        return [
            FirstModel::class => [
                'query_builder' => fn ($query, $columns = []) => $query->select([
                    'id',
                    'dependency_id',
                    ...$columns
                ]),
            ],
        ]
    }
}
```

```
use Comhon\MorphedModelExporter\Facades\MorphedModelExporter;

MorphedModelExporter::loadMorphedModels($myModels, 'myMorphToRelation', ['my_column']);
```

### Export morphed models

[](#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 customize export according a certain context :

```
class MyMorphedModelsExporters
{
    public function __invoke()
    {
        return [
            FirstModel::class => [
                'model_exporter' => fn ($model, $exportPrivate = false) => $exportPrivate
                    ? ['id' => $model->id, 'private' => $model->is_private]
                    : ['id' => $model->id],
            ],
        ]
    }
}
```

```
use Comhon\MorphedModelExporter\Facades\MorphedModelExporter;

$exportPrivate = true;
MorphedModelExporter::exportModel($morphedModel, $exportPrivate);
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [jean-philippe](https://github.com/comhon-project)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance77

Regular maintenance activity

Popularity13

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~24 days

Total

5

Last Release

265d ago

PHP version history (2 changes)v1.0.0PHP ^8.3

v1.0.1PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/c289f48fcf9ae241ba1fd30c2072026fae28e48b7e4848c2ee9fd4cc85cc6e61?d=identicon)[comhon](/maintainers/comhon)

---

Top Contributors

[![comhon-project](https://avatars.githubusercontent.com/u/14244177?v=4)](https://github.com/comhon-project "comhon-project (4 commits)")[![jeanphilippe-exatech](https://avatars.githubusercontent.com/u/247553054?v=4)](https://github.com/jeanphilippe-exatech "jeanphilippe-exatech (4 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (1 commits)")

---

Tags

laravelcomhon-projectlaravel-morphed-model-exporter

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/comhon-project-laravel-morphed-model-exporter/health.svg)

```
[![Health](https://phpackages.com/badges/comhon-project-laravel-morphed-model-exporter/health.svg)](https://phpackages.com/packages/comhon-project-laravel-morphed-model-exporter)
```

###  Alternatives

[andreaselia/laravel-api-to-postman

Generate a Postman collection automatically from your Laravel API

1.0k586.2k3](/packages/andreaselia-laravel-api-to-postman)[mll-lab/laravel-graphiql

Easily integrate GraphiQL into your Laravel project

683.2M9](/packages/mll-lab-laravel-graphiql)[scalar/laravel

Render your OpenAPI-based API reference

6183.9k2](/packages/scalar-laravel)[ryangjchandler/bearer

Minimalistic token-based authentication for Laravel API endpoints.

8129.8k](/packages/ryangjchandler-bearer)[combindma/laravel-facebook-pixel

Meta pixel integration for Laravel

4956.9k](/packages/combindma-laravel-facebook-pixel)[stechstudio/laravel-hubspot

A Laravel SDK for the HubSpot CRM Api

2971.0k](/packages/stechstudio-laravel-hubspot)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
