Presenter

laracasts/presenter image

Presenter stats

Downloads
2.2M
Stars
864
Open Issues
24
Forks
109

View on GitHub →

Simple view presenters

Easy View Presenters

So you have those scenarios where a bit of logic needs to be performed before some data (likely from your entity) is displayed from the view.

  • Should that logic be hard-coded into the view? No.
  • Should we instead store the logic in the model? No again!

Instead, leverage view presenters. That's what they're for! This package provides one such implementation.

Install

Pull this package in through Composer.

{
"require": {
"laracasts/presenter": "0.1.*"
}
}

Usage

The first step is to store your presenters somewhere - anywhere. These will be simple objects that do nothing more than format data, as required.

Here's an example of a presenter.

use Laracasts\Presenter\Presenter;
 
class UserPresenter extends Presenter {
 
public function fullName()
{
return $this->first . ' ' . $this->last;
}
 
public function accountAge()
{
return $this->created_at->diffForHumans();
}
 
}

Next, on your entity, pull in the Laracasts\Presenter\PresentableTrait trait, which will automatically instantiate your presenter class.

Here's an example - maybe a Laravel User model.

<?php
 
use Laracasts\Presenter\PresentableTrait;
 
class User extends \Eloquent {
 
use PresentableTrait;
 
protected $presenter = 'UserPresenter';
 
}

That's it! You're done. Now, within your view, you can do:

<h1>Hello, {{ $user->present()->fullName }}</h1>

Notice how the call to the present() method (which will return your new or cached presenter object) also provides the benefit of making it perfectly clear where you must go, should you need to modify how a full name is displayed on the page.

Have fun!

Jeffrey @ https://laracasts.com

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Laracasts Presenter Related Articles

Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Blastup logo

Blastup

Blastup provides social media enhancement services including buying Instagram likes, followers, and views, with features like instant delivery and a variety of packages to suit different needs.

Blastup
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
Statamic logo

Statamic

The drop-in ready Laravel CMS you’re been waiting for. Go full-stack or headless, flat file or database – it’s up to you.

Statamic