datacreativa / laravel-presentable
Create presenters for Eloquent Models
Installs: 16 599
Dependents: 0
Suggesters: 0
Security: 0
Stars: 70
Watchers: 3
Forks: 7
Open Issues: 3
Requires
- php: ^8.1
- illuminate/support: ^10.0
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^9.5.10
This package is not auto-updated.
Last update: 2024-11-14 00:44:46 UTC
README
This package allows the information to be presented in a different way by means of methods that can be defined in the model's presenter class.
Installation
You can install the package via composer:
composer require datacreativa/laravel-presentable
Usage
Use the HasPresentable
trait in any Eloquent Model, you must also add the $presenter
property.
use App\Models\Presenters\UserPresenter; use TheHiveTeam\Presentable\HasPresentable; class User extends Model { use HasPresentable; protected $presenter = UserPresenter::class; }
You can generate a class presenter with this command:
php artisan make:presenter UserPresenter
In this class you can define any method, for example:
namespace App\Models\Presenters; use TheHiveTeam\Presentable\Presenter; class UserPresenter extends Presenter { public function name() { return ucwords($this->model->name); } }
Now you have available the present()
method in Eloquent Model.
$user = (new User)->setAttribute('name', 'john doe'); $user->present()->name; // John Doe
Enjoy it!
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.