PHPackages                             thepublicgood/is-presentable - 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. thepublicgood/is-presentable

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

thepublicgood/is-presentable
============================

Simple Laravel model presenter trait

v2.0.1(4y ago)67.5k↓33.3%1MITPHPPHP ^8.0

Since Mar 8Pushed 2y ago1 watchersCompare

[ Source](https://github.com/tpg/is-presentable)[ Packagist](https://packagist.org/packages/thepublicgood/is-presentable)[ RSS](/packages/thepublicgood-is-presentable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (4)Versions (9)Used By (0)

IsPresentable for Laravel
=========================

[](#ispresentable-for-laravel)

[![Tests](https://github.com/tpg/is-presentable/actions/workflows/php.yml/badge.svg)](https://github.com/tpg/is-presentable/actions/workflows/php.yml)

IsPresentable is a simple package to help you format you Laravel model's data so that it's presentable in your views. For example, if you needed to print the creation date of a model in a Blade view, and you wrote:

```
{{ $model->created_at }}
```

You'd get HTML output that looks a little this this:

```
2021-12-09 03:04:22

```

That's fine, but it's not great. What if you wanted to format it? Well, you could do this:

```
{{ $model->created_at->format('d F Y H:i a') }}

```

Laravel automatically hands the `created_at` timestamp to `Carbon` so this actually works nicely. But what if you need to use the same format in a whole lot of places. Now it gets frustrating. You could create a model accessor, which would work just fine, but then it feels like it's litering up your model with presentation data. And do you add the same accessor to all your models? This is where IsPresentable comes in.

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

[](#installation)

As always, install into your Laravel app using Composer:

```
composer require thepublicgood/is-presentable=^2.0

```

Usage
-----

[](#usage)

> Version 2 brings a whole new approach to adding presentables to your models. The old version 1 way of using `presentable` methods still works fine and is backward compatible.

### Using `presentable` classes

[](#using-presentable-classes)

First add the `IsPresentable` trait to your model class:

```
namespace App\Models\Models;

use Illuminate\Database\Eloquent\Model;
use TPG\IsPresentable\Traits\IsPresentable;

class User extends Model {
    use IsPresentable;

    // ...
}
```

Now create a new presentable class for the attribute you'd like to present. Make sure to extend the `Presentable` abstract class. The model you are presenting will be injected as a `$model` class property so you can reference it with `$this->model`. Here's a simple `CreatedAtPresentable` class:

```
