No description
Find a file
2026-07-08 21:08:52 +00:00
src initial commit 2026-07-08 21:08:52 +00:00
.gitattributes initial commit 2026-07-08 21:08:52 +00:00
.gitignore initial commit 2026-07-08 21:08:52 +00:00
composer.json initial commit 2026-07-08 21:08:52 +00:00
LICENSE.md initial commit 2026-07-08 21:08:52 +00:00
README.md initial commit 2026-07-08 21:08:52 +00:00

Laravel Presenter

Minimal view-presenter base class plus a trait. The PresentableTrait adds a present() accessor to your model; the Presenter base class wraps the entity and exposes computed fields through property-style access. Tested on PHP 8.2+, framework-agnostic but designed to live alongside Laravel models.

Inspired by Jeffrey Way's original Laracasts Presenter package, modernized for PHP 8.2 and slimmed down.

Installation

composer require lithiumhosting/laravel-presenter

Usage

Declare a Presenter for the fields you want to compute:

use LithiumHosting\Presenter\Presenter;

class UserPresenter extends Presenter
{
    public function fullName(): string
    {
        return trim($this->entity->first_name.' '.$this->entity->last_name);
    }

    public function avatarUrl(): string
    {
        return 'https://gravatar.com/avatar/'.md5(strtolower($this->entity->email));
    }
}

Add the trait + presenter pointer to the model:

use LithiumHosting\Presenter\PresentableTrait;

class User extends Model
{
    use PresentableTrait;

    protected string $presenter = UserPresenter::class;
}

Then in views:

{{ $user->present()->fullName }}
<img src="{{ $user->present()->avatarUrl }}">

The presenter falls through to the underlying entity when no matching method exists, so $user->present()->email still returns the raw email column.

License

This package, laravel-presenter is licensed under The MIT License (MIT). Please see License File for more information.

Is it any good?

Yes.

When people first hear about a new product, they frequently ask if it is any good. A Hacker News user remarked:

Note to self: Starting immediately, all raganwald projects will have a "Is it any good?" section in the readme, and the answer shall be "yes.".