e Laravel Presentable package by Jonathan Zarate is a package to create presenter classes for Eloquent models. A presenter class allows you to separate display concerns from model data in a dedicated class.
Here's an example from the README:
1use App\Models\Presenters\UserPresenter;2use TheHiveTeam\Presentable\HasPresentable;3 4class User extends Model5{6 use HasPresentable;7 8 protected $presenter = UserPresenter::class;9}
Eloquent models with a presenter class use the HasPresentable
trait and a $presenter
property that defines the presenter class.
Here's an example of what a presenter class will look like:
1namespace App\Models\Presenters; 2 3use TheHiveTeam\Presentable\Presenter; 4 5class UserPresenter extends Presenter 6{ 7 public function name() 8 { 9 return ucwords($this->model->name);10 }11}
Using the User
model as an example, you can now access presenter methods in the view:
1$user->present()->name;
This approach might be overkill depending on the goals of your application; however, if you find that separating view display logic into a dedicated presenter class makes sense, this package could be an excellent fit.
You can learn more about this package, get full installation instructions, and view the source code on GitHub.
0 comments:
Post a Comment
Thanks