PHPackages                             coderflexx/laravel-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. [Database &amp; ORM](/categories/database)
4. /
5. coderflexx/laravel-presenter

ActiveLibrary[Database &amp; ORM](/categories/database)

coderflexx/laravel-presenter
============================

Write Clean/Reusable Code with Presenters.

v2.1.0(11mo ago)30113.9k—6.3%4[3 PRs](https://github.com/coderflexx/laravel-presenter/pulls)1MITPHPPHP ^8.0|^8.1CI passing

Since Feb 11Pushed 1mo agoCompare

[ Source](https://github.com/coderflexx/laravel-presenter)[ Packagist](https://packagist.org/packages/coderflexx/laravel-presenter)[ Docs](https://github.com/coderflexx/laravel-presenter)[ GitHub Sponsors](https://github.com/coderflex)[ RSS](/packages/coderflexx-laravel-presenter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (12)Versions (24)Used By (1)

Laravel Presenter
=================

[](#laravel-presenter)

[![The Latest Version on Packagist](https://camo.githubusercontent.com/8284ef3373708d36ba8000b51ce7a0145ee8b5d2fc33b0f169a63082ddb385b2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f646572666c6578782f6c61726176656c2d70726573656e7465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/coderflexx/laravel-presenter)[![GitHub Tests Action Status](https://camo.githubusercontent.com/0c6ddaf29c985ef1ce505d3c0e76a2fd6360da99c4c2ca9f405629982fd6eff5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636f646572666c6578782f6c61726176656c2d70726573656e7465722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473)](https://github.com/coderflexx/laravel-presenter/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/7c2e2db303e1b67ec0424cadc5acd89bbe049a22328116e351c146a0111d7e14/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f636f646572666c6578782f6c61726176656c2d70726573656e7465722f7068707374616e2e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65)](https://github.com/coderflexx/laravel-presenter/actions?query=workflow%3A%22Check+%26+fix+styling%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/d40c8d76f54ea46fdb6ec72b1ea25ed2ec20d16ba2677a1dbf155435d1668750/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f646572666c6578782f6c61726176656c2d70726573656e7465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/coderflexx/laravel-presenter)

A clean way to present your model attributes without putting them in the wrong file.

- [Laravel Presenter](#laravel-presenter)
    - [Installation](#installation)
    - [Usage](#usage)
    - [Model Implementation](#model-implementation)
    - [Create New Model Presenter class](#create-new-model-presenter-class)
    - [Using the `Presenter` Generated Class](#using-the-presenter-generated-class)
    - [Example](#example)
    - [Adding Another Presenter Type](#adding-another-presenter-type)
    - [Testing](#testing)
    - [Changelog](#changelog)
    - [Contributing](#contributing)
    - [Security Vulnerabilities](#security-vulnerabilities)
    - [Credits](#credits)
    - [License](#license)

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

[](#installation)

You can install the package via composer:

```
composer require coderflexx/laravel-presenter
```

You can publish the config file with:

```
php artisan vendor:publish --provider="Coderflex\LaravelPresenter\LaravelPresenterServiceProvider"
```

This is the contents of the published config file:

```
return [

    /*
    |--------------------------------------------------------------------------
    | Presenter Namespace
    |--------------------------------------------------------------------------
    |
    | This value informs LaravelPresenter which namespace you will be
    | selecting to store your presenters by default.
    | If this value equals to null, "App\Presenter" will be used
    | by default.
    |
    */

    'presenter_namespace'   => 'App\\Presenters',
];
```

Usage
-----

[](#usage)

The implementation of this package is so simple, all what you need to do is the following:

Model Implementation
--------------------

[](#model-implementation)

- Implement `CanPresent` Interface
- Use `UsesPresenters` Trait

```
use Coderflex\LaravelPresenter\Concerns\CanPresent;
use Coderflex\LaravelPresenter\Concerns\UsesPresenters;
// ...

class User extends Authenticatable implements CanPresent
{
    use UsesPresenters;

    // ...
}
```

Create New Model Presenter class
--------------------------------

[](#create-new-model-presenter-class)

This package gives you an easy way to generate new `Presenter` class, all you need to do is to use `presenter:make` command.

```
php artisan presenter:make UserPresenter
```

`UserPresenter` in our case, leaves by default in `App\Presenters`.

This is the contents of the `UserPresenter` file:

```
