PHPackages                             igaster/eloquent-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. [Utility &amp; Helpers](/categories/utility)
4. /
5. igaster/eloquent-decorator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

igaster/eloquent-decorator
==========================

A simple Trait that implements the Decorator pattern on Laravel eloquent models.

v1.0.2(6y ago)46.3k21MITPHP

Since Dec 29Pushed 6y ago4 watchersCompare

[ Source](https://github.com/igaster/eloquent-decorator)[ Packagist](https://packagist.org/packages/igaster/eloquent-decorator)[ Docs](https://github.com/igaster/eloquent-decorator.git)[ RSS](/packages/igaster-eloquent-decorator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (7)Versions (4)Used By (1)

Description
-----------

[](#description)

[![Laravel](https://camo.githubusercontent.com/5a1939c3fb1f6760124421638f59ab4ed15b6d03dbb30a6824df9af781880437/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d352e782d6f72616e67652e737667)](http://laravel.com)[![Downloads](https://camo.githubusercontent.com/867d075de4b649f8f9a5a3cd44c694e0a2335e2b0ee63fcfa310d73b5130a763/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696761737465722f656c6f7175656e742d6465636f7261746f722e737667)](https://packagist.org/packages/igaster/eloquent-decorator)[![Build Status](https://camo.githubusercontent.com/c17c50dc99e4071ed07a196b86c2d336d572c172657ac5555168cfb725d0daa1/68747470733a2f2f7472617669732d63692e6f72672f696761737465722f656c6f7175656e742d6465636f7261746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/igaster/eloquent-decorator)[![License](https://camo.githubusercontent.com/4661abfe916186acde514558e7f040833cb63ba7098401a51ce339cbb2b4cf9e/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e737667)](https://tldrlegal.com/license/mit-license)

A simple Trait that implements the Decorator pattern on Laravel eloquent models.

Usage:
------

[](#usage)

- Extend a model without inheritance (Composition vs Inheritance)
- Create multiple variations of a model

Example case study:
-------------------

[](#example-case-study)

You often need multiple versions of each Image. Your base model (Image) handles Database, Relations etc.

Your decorated models (ie thumbnail, profileImage) may apply image manipulations to the original image to create variations. ie they can overide the src() method of the base class to point to a different file. All your decorated versions share the same record in the Database but my alter their representation/behavior!

More ideas:

- Base model: `User`. Decorated Objects: Member, Moderator, SuperUser etc
- Base model: `Article`. Decorated Objects: FeaturedArticle, BlogPost,

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

[](#installation)

Edit your project's `composer.json` file to require:

```
"require": {
    "igaster/eloquent-decorator": "~1.0"
}

```

and install with `composer update

Implement the Decorator Pattern
-------------------------------

[](#implement-the-decorator-pattern)

#### Step 1: Class Decleration:

[](#step-1-class-decleration)

Your class should implement some interfaces to mimic the behavior of Eloquent models:

```
use igaster\EloquentDecorator\EloquentDecoratorTrait;

use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Queue\QueueableEntity;
use ArrayAccess;
use JsonSerializable;

class SuperUser implements ArrayAccess, Arrayable, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
{
    use EloquentDecoratorTrait;

    public $newProperty;		// Add a new property
    public $overridenPropery;	// or Overide a property (it hides it)

    public function TruncateDatabase(){
    	// add your functions...
    }
}
```

#### Step 2: Decorating an Eloquent model:

[](#step-2-decorating-an-eloquent-model)

```
	$user = User::find(1);

	$superUser = SuperUser::wrap($user);

	// you can think the SuperUser class as inhereted from the User model. so you can still do:

	$superUser->name = "Admin";
	$superUser->save();

	// But you also have access to SuperUser functions/attributes:

	$superUser->TruncateDatabase();

	// You can always access to the original (decorated object):
	$superUser->object->someProperty;
}
```

#### Need a constructor?

[](#need-a-constructor)

You have to override `EloquentDecoratorTrait` and create your own factory function. Dont forget to call `self::wrap($object)` at the end.

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 89.7% 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 ~691 days

Total

3

Last Release

2401d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/55bf1c9bc1f0553b439f18852299833373f96eb6c1342434c74fb61c5aecd508?d=identicon)[igaster](/maintainers/igaster)

---

Top Contributors

[![igaster](https://avatars.githubusercontent.com/u/4586319?v=4)](https://github.com/igaster "igaster (26 commits)")[![chrisdicarlo](https://avatars.githubusercontent.com/u/3483368?v=4)](https://github.com/chrisdicarlo "chrisdicarlo (2 commits)")[![flxsource](https://avatars.githubusercontent.com/u/1785446?v=4)](https://github.com/flxsource "flxsource (1 commits)")

---

Tags

traitdecoratordecorator patterndecoratelaravel-5.xdecorate eloquentdecorate models

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/igaster-eloquent-decorator/health.svg)

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

###  Alternatives

[nette/robot-loader

🍀 Nette RobotLoader: high performance and comfortable autoloader that will search and autoload classes within your application.

89152.7M320](/packages/nette-robot-loader)[kkszymanowski/traitor

Add a trait use statement to existing PHP class

1305.2M16](/packages/kkszymanowski-traitor)[markbaker/enumhelper

A small library that provides Helper Traits for PHP 8.1 Enums

2819.4k1](/packages/markbaker-enumhelper)

PHPackages © 2026

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