/laravel-presenter

Laravel Model Presenter

Primary LanguagePHPMIT LicenseMIT

Laravel Model Presenter

Latest Version on Packagist GitHub Tests Action Status Total Downloads

A simple package to present your model attributes.

Installation

You can install the package via composer:

composer require smartisan/laravel-presenter

You can publish the config file with:

php artisan vendor:publish --provider="Smartisan\Presenter\PresenterServiceProvider" --tag="config"

Usage

  1. Create a new presenter class using the following command
php artisan make:presenter UserPresenter
  1. Prepare your model with the presenter trait
use Smartisan\Presenter\HasPresenter;
use App\Presenters\UserPresenter;

class User extends Model
{
    use HasPresenter;
    
    protected $presenter = UserPresenter::class;
}
  1. Add a new presenter method with your logic to the generated presenter class
namespace Smartisan\Presenter\Presenter;

use Smartisan\Presenter\Presenter;

class UserPresenter extends Presenter
{
    public function fullName($value)
    {
        return $this->model->firstName . ' ' . $this->model->lastName;
    }
}
  1. Now to present the full name of the user you can do the following
$user->present()->fullName;

// or

$user->present()->full_name;

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.