PHPackages                             mindtwo/laravel-decorator - 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. [Database &amp; ORM](/categories/database)
4. /
5. mindtwo/laravel-decorator

ActivePackage[Database &amp; ORM](/categories/database)

mindtwo/laravel-decorator
=========================

Decorators for Eloquent Models in Laravel

3.0.3(1y ago)2166[1 PRs](https://github.com/mindtwo/laravel-decorator/pulls)1MITPHPPHP ^8.0

Since Dec 17Pushed 1y ago4 watchersCompare

[ Source](https://github.com/mindtwo/laravel-decorator)[ Packagist](https://packagist.org/packages/mindtwo/laravel-decorator)[ RSS](/packages/mindtwo-laravel-decorator/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (2)Versions (21)Used By (1)

Laravel Decorator
=================

[](#laravel-decorator)

[![Build Status](https://camo.githubusercontent.com/4d8997b924244aabcedacc53c288c5bede5e8be6dd0dbcf39d1f5a4b94f817b0/68747470733a2f2f7472617669732d63692e6f72672f6d696e6474776f2f6c61726176656c2d6465636f7261746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mindtwo/laravel-decorator)[![StyleCI](https://camo.githubusercontent.com/0425a4ca1db7585756516359fce05ffe354d36cfa9b11b94423796e1d382d50d/68747470733a2f2f7374796c6563692e696f2f7265706f732f3232383631363532392f736869656c64)](https://styleci.io/repos/159368194)[![Quality Score](https://camo.githubusercontent.com/0d35b20f350749e501d887f8d52c817c9cbd63bc5886080251d1e08a3aebf09e/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d696e6474776f2f6c61726176656c2d6465636f7261746f722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/mindtwo/laravel-decorator)[![Latest Stable Version](https://camo.githubusercontent.com/cbd559afdb24291086fb3e199d307560a2904e1f8cf7eb5996746a902186a9b5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d696e6474776f2f6c61726176656c2d6465636f7261746f723f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mindtwo/laravel-decorator)[![Total Downloads](https://camo.githubusercontent.com/076b72b5bfb3f9a32cb677e48a8b9e3d310c73c6f61ec303f005b825a03f31c2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d696e6474776f2f6c61726176656c2d6465636f7261746f723f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mindtwo/laravel-decorator)[![MIT Software License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require mindtwo/laravel-decorator
```

How to use?
-----------

[](#how-to-use)

### Preparing the Eloquent Model

[](#preparing-the-eloquent-model)

To use a decorator the underlaying eloquent model must implement the `Decoratable` interface. Farther you should use the `HasDecorator` trait, which implements the required methods.

```
use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDecorator\Interfaces\Decoratable;
use mindtwo\LaravelDecorator\Traits\HasDecorator;

class MyModel extends Model implements Decoratable
{
    use HasDecorator;
}
```

You can optionally setup a default decorator on the eloquent model, which will be used when you call the `decorate()` method without any params.

```
use Illuminate\Database\Eloquent\Model;
use mindtwo\LaravelDecorator\Interfaces\Decoratable;
use mindtwo\LaravelDecorator\Traits\HasDecorator;

class MyModel extends Model implements Decoratable
{
    use HasDecorator;

    /**
     * Return the default decorator full qualified class name.
     *
     * @return string
     */
    public function defaultDecorator(): string
    {
        return MyDecorator::class;
    }
}
```

### Writing a Decorator

[](#writing-a-decorator)

To write a decorator simply extend the basic decorator class. You can access the undelaying eloquent model by the `$this->model` property. Whenever you try to access a property on the decorator, it will first look for a function with the camilzed property name. If it is defined, it will be called, otherwise it will be forwarded to the underlaying eloquent model.

```
use mindtwo\LaravelDecorator\Decorator;

class MyDecorator extends Decorator
{
    /**
     * Get formatted creation date.
     *
     * @return string
     */
    public function defaultDecorator(): string
    {
        return $this->model->created_at->format('Y-m-d');
    }
}
```

### Using a Decorator

[](#using-a-decorator)

To use a decorator simply call the `decorate()` method on the model. You can use the full qualified class name of a decorator class as parameter to specify a decorator, otherwise the default decorator will be used.

```
$myObject = MyModel::make();

// Use a certain decorator
$myDecoratedObject = $myObject->decorate(MyDecorator::class);

// Use the default decorator (needs to be defined on the model)
$myDecoratedObject = $myObject->decorate();
```

It is also possible to call the 'decorate()' method on collections, cause the package autmatically registers it as a macro.

```
$myCollection = MyModel::get();

// Use a certain decorator
$myDecoratedCollection = $myCollection->decorate(MyDecorator::class);

// Use the default decorator (needs to be defined on the model)
$myDecoratedCollection = $myCollection->decorate();
```

Note that all items in the collection must implement the `Decoratable` interface, otherwise this will throw an exception.

### Changelog

[](#changelog)

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

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

[](#contributing)

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

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [mindtwo GmbH](https://github.com/mindtwo)
- [All Other Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

38

—

LowBetter than 84% of packages

Maintenance42

Moderate activity, may be stable

Popularity13

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 50% of commits — single point of failure

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 ~119 days

Recently: every ~250 days

Total

16

Last Release

541d ago

Major Versions

1.4.3 → 2.02022-01-25

2.2 → 3.02024-02-29

PHP version history (5 changes)1.0PHP ^7.2

1.4PHP ^7.4

1.4.1PHP ^7.3

1.4.3PHP ^7.0|^8.0

3.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4cc86fe6179314d204b14d1c81eb09a87ef84b0bcf2360dcd981171d1346c077?d=identicon)[mindtwo](/maintainers/mindtwo)

---

Top Contributors

[![tkivelip](https://avatars.githubusercontent.com/u/3762670?v=4)](https://github.com/tkivelip "tkivelip (16 commits)")[![jonasemde](https://avatars.githubusercontent.com/u/5083193?v=4)](https://github.com/jonasemde "jonasemde (9 commits)")[![blumewas](https://avatars.githubusercontent.com/u/5960093?v=4)](https://github.com/blumewas "blumewas (6 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (1 commits)")

### Embed Badge

![Health badge](/badges/mindtwo-laravel-decorator/health.svg)

```
[![Health](https://phpackages.com/badges/mindtwo-laravel-decorator/health.svg)](https://phpackages.com/packages/mindtwo-laravel-decorator)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M542](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M209](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

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