A simple package to present your model attributes.
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"
- Create a new presenter class using the following command
php artisan make:presenter UserPresenter
- 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;
}
- 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;
}
}
- Now to present the full name of the user you can do the following
$user->present()->fullName;
// or
$user->present()->full_name;
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.