PHPackages                             davidianbonner/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. [Templating &amp; Views](/categories/templating)
4. /
5. davidianbonner/presenter

ActiveLibrary[Templating &amp; Views](/categories/templating)

davidianbonner/presenter
========================

A Laravel presenter and transformer package.

2.0.1(6y ago)29.1kMITPHPPHP ^7.0CI failing

Since Oct 20Pushed 6y ago1 watchersCompare

[ Source](https://github.com/davidianbonner/presenter)[ Packagist](https://packagist.org/packages/davidianbonner/presenter)[ RSS](/packages/davidianbonner-presenter/feed)WikiDiscussions master Synced 2w ago

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

Presenter/Transformer
=====================

[](#presentertransformer)

[![Build Status](https://camo.githubusercontent.com/3e88e457a5bd060b8953cc9f81d520a81616ba7648c6968f687645e32e26d355/68747470733a2f2f7472617669732d63692e6f72672f646176696469616e626f6e6e65722f70726573656e7465722e7376673f6272616e63683d616e616c797369732d7a3445674b6e)](https://travis-ci.org/davidianbonner/presenter)[![StyleCI](https://camo.githubusercontent.com/a0740a8124f7b825e25c112626bfb37dad420e12f0e9857034e1891c9c514e40/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130373638323738342f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/107682784)

A simple data presenter/transformer for Laravel applications that can be used with views and JSON.

In most cases, the data transformation required to output JSON via a REST API is the same as the data required in our views. This package prevents having to use transformers for JSON (i.e. with Fractal – great for larger apps and complex transformations) and attribute mutators. Whether returning a view or JSON, the data can be presented and transformed using the one class.

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

[](#installation)

Install the package through Composer:

```
composer require davidianbonner/presenter
```

Laravel 5.5+ uses Package Auto-Discovery which doesn't require you to manually add the ServiceProvider. However if you are using 5.4 or earlier, add the package service provider in the `providers` array in `config/app.php`:

```
DavidIanBonner\Presenter\PresenterServiceProvider::class
```

Add the facade to your `aliases` in `config/app.php:

```
'Presenter' => DavidIanBonner\Presenter\Facades\Presenter::class,
```

Then publish the config file:

```
php artisan vendor:publish --provider="DavidIanBonner\Presenter\PresenterServiceProvider"

```

How it works
------------

[](#how-it-works)

A 'transformation' will only take place if the object implements the `Presentable` interface.

If the presentable object is an eloquent model, the presenter will check for loaded relationships and attempt to transform them as well, replacing the existing relationship with the transformer object.

#### Present/transform an object

[](#presenttransform-an-object)

When transformed, the object will injected into a transformer class which will give you access to magic methods, magic getters on the object and automatic `toArray` and `toJson` output.

```
// Transform an object
Presenter::transform(Book::find(1), BookTransformer::class);

// or with pre-set transformers
Presenter::transform(Book::find(1));
```

#### Pre-set transformers

[](#pre-set-transformers)

Set your presentable =&gt; transformer relations in `config/presenter.php`:

```
'transformers' => [
    App\Models\Book::class => App\Transformers\BookTransformer::class,
    App\OtherObject\Foo::class => App\Transformers\FooTransformer::class,
],
```

#### Transformers

[](#transformers)

Transformers must extend `DavidIanBonner\Presenter\Transformer`. A transformer can utilise mutated attribute methods in a similar manner to eloquent.

```
