PHPackages                             ivan88217/laravel-auto-presenter - 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. ivan88217/laravel-auto-presenter

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

ivan88217/laravel-auto-presenter
================================

A system for auto-decorating models with presenter objects.

0772PHP

Since Dec 25Pushed 2y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Laravel Auto Presenter 7
========================

[](#laravel-auto-presenter-7)

This package automatically decorates objects bound to views during the view render process.

[![Banner](https://user-images.githubusercontent.com/2829600/71563899-63b94e80-2a8f-11ea-9c5a-18e5484405a9.png)](https://user-images.githubusercontent.com/2829600/71563899-63b94e80-2a8f-11ea-9c5a-18e5484405a9.png)

[![Build Status](https://camo.githubusercontent.com/2968e5bfe891914bbda3f9a88d7f2767c8bc5bf239fa7ffb0f23b2017ea9c35f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c61726176656c2d6175746f2d70726573656e7465722f6c61726176656c2d6175746f2d70726573656e7465722f54657374733f6c6162656c3d5465737473267374796c653d666c61742d737175617265)](https://github.com/laravel-auto-presenter/laravel-auto-presenter/actions?query=workflow%3ATests)[![StyleCI Status](https://camo.githubusercontent.com/c1faa32b7a6b1432989c2f9f76f1388dc7302ae5aa2a2107e9b298213073dcce/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f31323033343730312f736869656c64)](https://github.styleci.io/repos/12034701)[![Software License](https://camo.githubusercontent.com/c090e080484e2a2bc766446291d04437db823929042bf614b26a1643660ddf6f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e3f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist Downloads](https://camo.githubusercontent.com/a895d2301e23ccc7f306c7888f73be6558128dd86c27c0ee115ec44c77e378ec/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d63636f6f6c2f6c61726176656c2d6175746f2d70726573656e7465723f7374796c653d666c61742d737175617265)](https://packagist.org/packages/mccool/laravel-auto-presenter)[![Latest Version](https://camo.githubusercontent.com/c7103f6924205ac815d5cbaebc91ed49113126ae23f6839cb886f62b675c2291/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6c61726176656c2d6175746f2d70726573656e7465722f6c61726176656c2d6175746f2d70726573656e7465723f7374796c653d666c61742d737175617265)](https://github.com/laravel-auto-presenter/laravel-auto-presenter/releases)

Features
--------

[](#features)

- Automatically decorate objects bound to views
- Automatically decorate objects within paginator instances
- Automatically decorate objects within arrays and collections

Installing
----------

[](#installing)

Laravel Auto Presenter requires [PHP](https://php.net) 7.1-8.1 and supports Laravel 5.5-9. If you need support for older Laravel or PHP versions, please choose an older version of Laravel Auto Presenter.

Auto PresenterL5.1L5.2L5.3L5.4L5.5L5.6L5.7L5.8L6L7L8L94.3✅✅✅❌❌❌❌❌❌❌❌❌5.0✅✅✅✅❌❌❌❌❌❌❌❌6.2✅✅✅✅✅❌❌❌❌❌❌❌7.7❌❌❌❌✅✅✅✅✅✅✅✅To get the latest version, simply require the project using [Composer](https://getcomposer.org):

```
$ composer require mccool/laravel-auto-presenter
```

Once installed, if you are not using automatic package discovery, then you need to register the `McCool\LaravelAutoPresenter\AutoPresenterServiceProvider` service provider in your `config/app.php`.

You can also optionally alias our facade:

```
        'AutoPresenter' => McCool\LaravelAutoPresenter\Facades\AutoPresenter::class,
```

Upgrading
---------

[](#upgrading)

### Version 6 to 7

[](#version-6-to-7)

In Laravel Auto Presenter 7, note that:

- Laravel 5.5-8 are supported now. Use V6 if you need L5.1-5.4.
- Our new minimum PHP version requirement is 7.1.3, up from 7.0.0.

### Version 5 to 6

[](#version-5-to-6)

Going from Laravel Auto Presenter 5, to 6, note that:

- We have a new `Decoratable` interface to determine if relations can be decorated. While this is not a BC break, since `HasPresenter` extends it, it is definitely worth noting.
- v6 supports Laravel 5.5 now, without dropping support for 5.1+.
- Our new minimum PHP version requirement is 7.0.0, up from 5.5.9.

### Version 4 to 5

[](#version-4-to-5)

If you're upgrading from Laravel Auto Presenter 4, to 5, note that:

- The `BasePresenter` no longer has a constructor, so you cannot call `parent::__construct($resource)`.
- The model is now injected using the `setWrappedObject` method, inherited from the `BasePresenter`.
- V5 now supports Laravel 5.4 as well as 5.1, 5.2, and 5.3.

Usage
-----

[](#usage)

To show how it's used, we'll pretend that we have an Eloquent Post model. It doesn't have to be Eloquent, it could be any kind of class. But, this is a normal situation. The Post model represents a blog post.

I'm using really basic code examples here, so just focus on how the auto-presenter is used and ignore the rest.

```
use Example\Accounts\User;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table = 'posts';
    protected $fillable = ['author_id', 'title', 'content', 'published_at'];

    public function author()
    {
        return $this->belongsTo(User::class, 'author_id');
    }
}
```

Also, we'll need a controller..

```
use Example\Accounts\Post;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\View;

class PostsController extends Controller
{
    public function getIndex()
    {
        $posts = Post::all();
        return View::make('posts.index', compact('posts'));
    }
}
```

and a view...

```
@foreach($posts as $post)
    {{ $post->title }} - {{ $post->published_at }}
@endforeach
```

In this example the published\_at attribute is likely to be in the format: "Y-m-d H:i:s" or "2013-08-10 10:20:13". In the real world this is not what we want in our view. So, let's make a presenter that lets us change how the data from the Post class is rendered within the view.

```
use Carbon\Carbon;
use Example\Accounts\Post;
use McCool\LaravelAutoPresenter\BasePresenter;

class PostPresenter extends BasePresenter
{
    public function published_at()
    {
        $published = $this->wrappedObject->published_at;

        return Carbon::createFromFormat('Y-m-d H:i:s', $published)
            ->toFormattedDateString();
    }
}
```

*Note that the model is injected by calling the `setWrappedObject` method, inherited from `BasePresenter`.*

We need the post class to implement the interface.

```
use Example\Accounts\User;
use Example\Blog\PostPresenter;
use McCool\LaravelAutoPresenter\HasPresenter;
use Illuminate\Database\Eloquent\Model;

class Post extends Model implements HasPresenter
{
    protected $table = 'posts';
    protected $fillable = ['author_id', 'title', 'content', 'published_at'];

    public function author()
    {
        return $this->belongsTo(User::class, 'author_id');
    }

    public function getPresenterClass()
    {
        return PostPresenter::class;
    }
}
```

Now, with no additional changes our view will show the date in the desired format.

**The `Decoratable` interface is used to allow the model's relations to be decorated, and the `HasPresenter` interface (which extends that one) is used to have the model itself decorated.**

Troubleshooting
---------------

[](#troubleshooting)

If an object isn't being decorated correctly in the view then there's a good chance that it's simply not in existence when the view begins to render. For example, lazily-loaded relationships won't be decorated. You can fix this by eager-loading them instead. Auth::user() will never be decorated. I prefer to bind $currentUser to my views, anyway.

If an object is a relation of another object and it isn't being decorated in the view, you might not have added the `Decoratable` interface to the other object. To fix this, add the `Decoratable` interface to the other object.

Security
--------

[](#security)

If you discover a security vulnerability within this package, please send an email to . All security vulnerabilities will be promptly addressed. You may view our full security policy [here](https://github.com/laravel-auto-presenter/laravel-auto-presenter/security/policy).

License
-------

[](#license)

Laravel Auto Presenter is licensed under [The MIT License (MIT)](LICENSE).

For Enterprise
--------------

[](#for-enterprise)

Available as part of the Tidelift Subscription

The maintainers of `mccool/laravel-auto-presenter` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/packagist-mccool-laravel-auto-presenter?utm_source=packagist-mccool-laravel-auto-presenter&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)

###  Health Score

19

—

LowBetter than 9% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity20

Early-stage or recently created project

 Bus Factor1

Top contributor holds 83.3% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/6d188115b6e53edfecaaf711164275c487aa9b1b7cb03d27368c43f216bbe018?d=identicon)[ivan88217](/maintainers/ivan88217)

---

Top Contributors

[![GrahamCampbell](https://avatars.githubusercontent.com/u/2829600?v=4)](https://github.com/GrahamCampbell "GrahamCampbell (245 commits)")[![kirkbushell](https://avatars.githubusercontent.com/u/65171?v=4)](https://github.com/kirkbushell "kirkbushell (23 commits)")[![dq-ivan-shi](https://avatars.githubusercontent.com/u/165010653?v=4)](https://github.com/dq-ivan-shi "dq-ivan-shi (7 commits)")[![rquadling](https://avatars.githubusercontent.com/u/12801?v=4)](https://github.com/rquadling "rquadling (2 commits)")[![rtablada](https://avatars.githubusercontent.com/u/2532004?v=4)](https://github.com/rtablada "rtablada (2 commits)")[![mrsimonbennett](https://avatars.githubusercontent.com/u/1471305?v=4)](https://github.com/mrsimonbennett "mrsimonbennett (2 commits)")[![jamierumbelow](https://avatars.githubusercontent.com/u/49284?v=4)](https://github.com/jamierumbelow "jamierumbelow (1 commits)")[![lguima](https://avatars.githubusercontent.com/u/1562097?v=4)](https://github.com/lguima "lguima (1 commits)")[![marijnz0r](https://avatars.githubusercontent.com/u/12230678?v=4)](https://github.com/marijnz0r "marijnz0r (1 commits)")[![Nyholm](https://avatars.githubusercontent.com/u/1275206?v=4)](https://github.com/Nyholm "Nyholm (1 commits)")[![patrickcarlohickman](https://avatars.githubusercontent.com/u/6036266?v=4)](https://github.com/patrickcarlohickman "patrickcarlohickman (1 commits)")[![stidges](https://avatars.githubusercontent.com/u/4399967?v=4)](https://github.com/stidges "stidges (1 commits)")[![balping](https://avatars.githubusercontent.com/u/5840038?v=4)](https://github.com/balping "balping (1 commits)")[![webcraft](https://avatars.githubusercontent.com/u/56675?v=4)](https://github.com/webcraft "webcraft (1 commits)")[![bl4ck-bird](https://avatars.githubusercontent.com/u/3314410?v=4)](https://github.com/bl4ck-bird "bl4ck-bird (1 commits)")[![bonroyage](https://avatars.githubusercontent.com/u/4411748?v=4)](https://github.com/bonroyage "bonroyage (1 commits)")[![DarkaOnLine](https://avatars.githubusercontent.com/u/1171698?v=4)](https://github.com/DarkaOnLine "DarkaOnLine (1 commits)")[![hardevine](https://avatars.githubusercontent.com/u/1171618?v=4)](https://github.com/hardevine "hardevine (1 commits)")[![hendore](https://avatars.githubusercontent.com/u/1441228?v=4)](https://github.com/hendore "hendore (1 commits)")

### Embed Badge

![Health badge](/badges/ivan88217-laravel-auto-presenter/health.svg)

```
[![Health](https://phpackages.com/badges/ivan88217-laravel-auto-presenter/health.svg)](https://phpackages.com/packages/ivan88217-laravel-auto-presenter)
```

PHPackages © 2026

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