A clean way to present your model attributes without putting them in the wrong file.
You can install the package via composer:
composer require coderflexx/laravel-presenterYou 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', ];The implementation of this package is so simple, all what you need to do is the following:
- Implement
CanPresentInterface - Use
UsesPresentersTrait
use Coderflex\LaravelPresenter\Concerns\CanPresent; use Coderflex\LaravelPresenter\Concerns\UsesPresenters; // ... class User extends Authenticatable implements CanPresent { use UsesPresenters; // ... }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 UserPresenterUserPresenter in our case, leaves by default in App\Presenters.
This is the contents of the UserPresenter file:
<?php namespace App\Presenters; use Coderflex\LaravelPresenter\Presenter; class UserPresenter extends Presenter { // }If you want to change the directory, you have two options.
First options is to set the full namespace while you're creating the presenter class
php artisna presenter:make App\Models\Presenter\UserPresenterOr change presenter_namespace from config/laravel-presenter file.
return [ ... 'presenter_namespace' => 'App\\Presenters', ... ];After you create the presenter class, you need to register it on the Model by adding the $presenters protected property:
use App\Presenters\UserPresenter; use Coderflex\LaravelPresenter\Concerns\CanPresent; use Coderflex\LaravelPresenter\Concerns\UsesPresenters; // ... class User extends Authenticatable implements CanPresent { use UsesPresenters; protected $presenters = [ 'default' => UserPresenter, ]; }By default, the type of your presenter class is default, but you can use as many of presenters you want, just by identifying the type in $presenters property.
Now, after we generated the presenter class, and we implemented it successfully in our model, we can use it like so:
In your UserPresenter class or any presenter class you generated.
... class UserPresenter extends Presenter { public function fullName() { return "{$this->model->first_name} {$this->model->last_name}"; } } ...We add a new method to present the fullName.
In your blade or any place you want, you can do:
$user->present()->fullNameYour application will show the full name from the method you added.
Like I said above, by default the type will be default but, you can add more types as you need.
Here is an example:
use App\Presenters\UserPresenter; use Coderflex\LaravelPresenter\Concerns\CanPresent; use Coderflex\LaravelPresenter\Concerns\UsesPresenters; // ... class User extends Authenticatable implements CanPresent { use UsesPresenters; protected $presenters = [ 'default' => UserPresenter, 'setting' => UserSettingPresenter, ]; }Generate new UserSettingPresenter
php artisan presenter:make UserSettingPresenterAdd anything to UserSettingPresenter method
... class UserSettingPresenter extends Presenter { public function lang() { return $this->model->settings->defaultLang; } } ...Finally, set setting as a type:
$user->present('setting')->lang;By that, you can split your logic and make your code base even cleaner.
composer testPlease 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.