Package Data | |
---|---|
Maintainer Username: | samueljtaylor |
Maintainer Contact: | sam@taylornetwork.ca (Sam Taylor) |
Package Create Date: | 2016-11-16 |
Package Last Update: | 2017-02-20 |
Language: | PHP |
License: | Unknown |
Last Refreshed: | 2025-02-09 15:02:47 |
Package Statistics | |
---|---|
Total Downloads: | 20 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Adds another place to put logic for models. Instead of cluttering up models with logic to display/format data, add them to a presenter!
This is a rewrite of laracasts/Presenter with some added functionality, commands, etc.
Via Composer
$ composer require taylornetwork/presenter
Add the service provider to the providers array in config/app.php
'providers' => [
TaylorNetwork\Presenter\PresenterServiceProvider::class,
],
$ php artisan vendor:publish
This will add config/presenter.php
where you can define the namespace you want your presenter classes to be stored.
Create a presenter using the artisan command, for example a presenter for the User model.
$ php artisan make:presenter UserPresenter
This will create a presenter class you can add logic to that you don't want in the model or the view. Model attributes are accessible via $this->model
use TaylorNetwork\Presenter\Presenter;
class UserPresenter extends Presenter
{
/**
* Get the user's full name
*
* @return string
*/
public function fullName()
{
return $this->model->first_name . ' ' . $this->model->last_name;
}
/**
* Get the time since the user signed up
*
* @return string
*/
public function userSince()
{
return $this->model->created_at->diffForHumans();
}
}
You will need to add the presentable trait and a $presenter
property to your model
use TaylorNetwork\Presenter\Presentable;
use App\Presenters\UserPresenter;
class User extends Model
{
use Presentable;
/**
* Presenter Class
*
* @var string
*/
protected $presenter = UserPresenter::class;
// Code
}
You can access the presenter with present()
<h1>{{ $user->present()->fullName }}, you signed up {{ $user->present()->userSince }}</h1>
The MIT License (MIT). Please see License File for more information.