PHPackages                             karl456/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. karl456/presenter

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

karl456/presenter
=================

Laravel View Presenters

12.0.0(7mo ago)03.2kMITPHPPHP ^8.2

Since Jun 12Pushed 7mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (1)Versions (16)Used By (0)

Laravel View Presenters
=======================

[](#laravel-view-presenters)

[![Source](https://camo.githubusercontent.com/13fce68e1666de90fb12f2ae048e6e08d46eec59b97c2a2f4ce683902277f9fc/687474703a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d6b61726c3435362f70726573656e7465722d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/karl456/presenter)[![License](https://camo.githubusercontent.com/30597ff9a350144f03bffdd9183e16468e0b3ca1193e1d08591d992622738d55/687474703a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://tldrlegal.com/license/mit-license)

Laravel view presenters.

Presenters allow you to manipulate any form of data for display within a view file. A simple example would be if you have a user entity with fields for the first and last names, how would you simply display the full name of the user within your view file? The most common solution would be something like the following:

```
Hello, {{ $user->first_name }} {{ $user->last_name }}!
```

Which works, but *every* time you need to display a user's full name, you'd have to type this out. What if instead it was something like this:

```
Hello, {{ $user->present()->fullName }}!
```

Isn't that much more readable than the previous example? Now you may argue that you could add this type of logic directly to your model class, *which you could*, but then you'll find that your model classes are riddled with methods that are manipulating view logic. I don't believe model entities should be responsible for this. Their **only job** is to pull the requested data from the database and hand it over. *That's it.*

Quick Installation
------------------

[](#quick-installation)

Begin by installing the package through Composer.

```
composer require karl456/presenter

```

And that's it! With your coffee in reach, start building out some awesome presenters!

Usage
-----

[](#usage)

### 1. Pull in trait

[](#1-pull-in-trait)

Within your model, simply pull in the `Karl456\Presenter\Traits\PresentableTrait` trait, which will automatically instantiate the Karl456 Presenter class.

```
...

use Karl456\Presenter\Traits\PresentableTrait;

class Example extends Eloquent
{
	use PresentableTrait;

	...
}
```

### 2. Define your presenter class

[](#2-define-your-presenter-class)

Define a protected `$presenter` variable pointing to the namespace of your presenter class.

```
...

use Karl456\Presenter\Traits\PresentableTrait;

class Example extends Eloquent
{
	use PresentableTrait;

	protected $presenter = App\Presenters\Page::class;

	...
}
```

### 3. Create your presenter class

[](#3-create-your-presenter-class)

Create a new class as defined within your model earlier - in our case we'll create a new directory within the `app` directory called `Presenters`, and create a `Page` file. Presenters should extend the abstract `Karl456\Presenter\Presenter` class.

```
