igaster/eloquent-decorator
Larablocks Analysis
igaster/eloquent-decorator works with Laravel 5 and 6 (latest release v1.0.2, Oct 11, 2019), all past end of life (the last on Sep 6, 2022): no more fixes, even for security.
Recommendation checks
Use with careUse with care: meets 0 of 4 checks.
- Supports only Laravel 5 and 6, past end of life since Sep 6, 2022
- No release in the last 12 months (last: Oct 11, 2019)
- 1 downloads a month (needs 10,000)
- 4 GitHub stars (needs 100)
State
Quiet- Last stable release: Oct 11, 2019.
- No releases in the last 12 months.
- First released: Dec 29, 2015.
- Monthly downloads: 1.
- 0 open issues on GitHub.
Data from Packagist and GitHub, checked by Larablocks on Dec 1, 2026.
| Install | |
|---|---|
composer require igaster/eloquent-decorator |
|
| Latest Version: | v1.0.2 |
| Laravel: | 5 · 6 |
| License: | MIT |
| Last Updated: | May 26, 2026 |
| Links: | GitHub · Packagist |
Description
A simple Trait that implements the Decorator pattern on Laravel eloquent models.
Usage:
- Extend a model without inheritance (Composition vs Inheritance)
- Create multiple variations of a model
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
Edit your project's composer.json file to require:
"require": {
"igaster/eloquent-decorator": "~1.0"
}
and install with `composer update
Implement the Decorator Pattern
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:
$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?
You have to override EloquentDecoratorTrait and create your own factory function. Dont forget to call self::wrap($object) at the end.
Related Packages
Decorate your objects using presenters. Primarily to keep presentation logic out...
A Laravel Eloquent Model trait for adding a UUID to Eloquent Models
A Laravel Eloquent Model trait for adding User Id's to Eloquent Models
Version History
| Version | Released | PHP | Laravel | License |
|---|---|---|---|---|
| v1.0.2 | 5.* || | MIT | ||
| v1.0.1 | >=5.4.0 | 5.* | MIT | |
| v1.0.0 | >=5.4.0 | 5.* | MIT |