Package Data | |
---|---|
Maintainer Username: | Cherry Pie |
Maintainer Contact: | 12fcv4@gmail.com (Yaro) |
Package Create Date: | 2017-09-11 |
Package Last Update: | 2021-02-22 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 15:09:10 |
Package Statistics | |
---|---|
Total Downloads: | 7,870 |
Monthly Downloads: | 6 |
Daily Downloads: | 0 |
Total Stars: | 2 |
Total Watchers: | 2 |
Total Forks: | 1 |
Total Open Issues: | 0 |
composer require yaro/presenter
Create presenter class and extend it from Yaro\Presenter\AbstractPresenter
. And specify $arrayable
keys, which presenter should use from model:
namespace App\Presenters;
use Yaro\Presenter\AbstractPresenter;
class UserPresenter extends AbstractPresenter
{
protected $arrayable = [
'name',
'profession'
];
}
Additionally you can add method for getting specific values:
protected $arrayable = [
'name',
'profession',
'random_number', // <- a virtual key, that model doesnt contain
];
// just studly case your key and surround it with 'get' and 'Present'.
public function getRandomNumberPresent()
{
return rand(11,22);
}
Include trait Yaro\Presenter\PresenterTrait
in your model. And specify presenter class.
use Yaro\Presenter\PresenterTrait;
class User
{
use PresenterTrait;
// ...
protected $presenter = \App\Presenters\UserPresenter::class;
}
Or override getPresenterClass
method, if you dont like protected attribute, or just need some extra logic:
class User
{
use PresenterTrait;
// ...
public function getPresenterClass()
{
if ($this->isBlocked()) {
return \App\Presenters\BlockedUserPresenter::class;
}
return \App\Presenters\UserPresenter::class;
}
}
And just send it to output:
$user = User::first();
return response()->json(compact('user'));
{
"name": "Davy Jones",
"profession": "pirate",
"random_number": 13
}
The MIT License (MIT). Please see LICENSE for more information.