mccool / laravel-auto-presenter
A system for auto-decorating models with presenter objects.
Fund package maintenance!
GrahamCampbell
Tidelift
Installs: 1 030 681
Dependents: 14
Suggesters: 0
Security: 0
Stars: 753
Watchers: 21
Forks: 73
Open Issues: 5
Requires
- php: ^7.1.3 || ^8.0
- illuminate/container: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0
- illuminate/contracts: ^5.5 || ^6.0 || ^7.0 || ^8.0|| ^9.0
- illuminate/events: ^5.5 || ^6.0 || ^7.0 || ^8.0|| ^9.0
- illuminate/pagination: ^5.5 || ^6.0 || ^7.0 || ^8.0|| ^9.0
- illuminate/support: ^5.5 || ^6.0 || ^7.0 || ^8.0|| ^9.0
- illuminate/view: ^5.5 || ^6.0 || ^7.0 || ^8.0|| ^9.0
Requires (Dev)
- graham-campbell/analyzer: ^2.4 || ^3.0
- graham-campbell/testbench: ^5.4
- mockery/mockery: ^1.3.1
- phpunit/phpunit: ^6.5.14 || ^7.5.20 || ^8.5.8 || ^9.3.7
- dev-master / 7.7.x-dev
- 7.7.0
- 7.6.0
- 7.5.0
- 7.4.1
- 7.4.0
- 7.3.1
- 7.3.0
- 7.2.1
- 7.2.0
- 7.1.0
- 7.0.1
- 7.0.0
- 6.2.x-dev
- 6.2.1
- 6.2.0
- 6.1.0
- 6.0.0
- 5.0.x-dev
- 5.0.0
- 4.3.x-dev
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 3.1.x-dev
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 3.0.0-beta8
- 3.0.0-beta7
- 3.0.0-beta6
- 3.0.0-beta5
- 3.0.0-beta4
- 3.0.0-beta3
- 2.2.x-dev
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.0
- 2.0.0
- 1.2.x-dev
- 1.2.0
- 1.1.0
- 1.0.0
This package is auto-updated.
Last update: 2024-10-09 15:20:21 UTC
README
This package automatically decorates objects bound to views during the view render process.
Features
- Automatically decorate objects bound to views
- Automatically decorate objects within paginator instances
- Automatically decorate objects within arrays and collections
Installing
Laravel Auto Presenter requires PHP 7.1-8.1 and supports Laravel 5.5-9. If you need support for older Laravel or PHP versions, please choose an older version of Laravel Auto Presenter.
To get the latest version, simply require the project using Composer:
$ composer require mccool/laravel-auto-presenter
Once installed, if you are not using automatic package discovery, then you need to register the McCool\LaravelAutoPresenter\AutoPresenterServiceProvider
service provider in your config/app.php
.
You can also optionally alias our facade:
'AutoPresenter' => McCool\LaravelAutoPresenter\Facades\AutoPresenter::class,
Upgrading
Version 6 to 7
In Laravel Auto Presenter 7, note that:
- Laravel 5.5-8 are supported now. Use V6 if you need L5.1-5.4.
- Our new minimum PHP version requirement is 7.1.3, up from 7.0.0.
Version 5 to 6
Going from Laravel Auto Presenter 5, to 6, note that:
- We have a new
Decoratable
interface to determine if relations can be decorated. While this is not a BC break, sinceHasPresenter
extends it, it is definitely worth noting. - v6 supports Laravel 5.5 now, without dropping support for 5.1+.
- Our new minimum PHP version requirement is 7.0.0, up from 5.5.9.
Version 4 to 5
If you're upgrading from Laravel Auto Presenter 4, to 5, note that:
- The
BasePresenter
no longer has a constructor, so you cannot callparent::__construct($resource)
. - The model is now injected using the
setWrappedObject
method, inherited from theBasePresenter
. - V5 now supports Laravel 5.4 as well as 5.1, 5.2, and 5.3.
Usage
To show how it's used, we'll pretend that we have an Eloquent Post model. It doesn't have to be Eloquent, it could be any kind of class. But, this is a normal situation. The Post model represents a blog post.
I'm using really basic code examples here, so just focus on how the auto-presenter is used and ignore the rest.
use Example\Accounts\User; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $table = 'posts'; protected $fillable = ['author_id', 'title', 'content', 'published_at']; public function author() { return $this->belongsTo(User::class, 'author_id'); } }
Also, we'll need a controller..
use Example\Accounts\Post; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\View; class PostsController extends Controller { public function getIndex() { $posts = Post::all(); return View::make('posts.index', compact('posts')); } }
and a view...
@foreach($posts as $post) <li>{{ $post->title }} - {{ $post->published_at }}</li> @endforeach
In this example the published_at attribute is likely to be in the format: "Y-m-d H:i:s" or "2013-08-10 10:20:13". In the real world this is not what we want in our view. So, let's make a presenter that lets us change how the data from the Post class is rendered within the view.
use Carbon\Carbon; use Example\Accounts\Post; use McCool\LaravelAutoPresenter\BasePresenter; class PostPresenter extends BasePresenter { public function published_at() { $published = $this->wrappedObject->published_at; return Carbon::createFromFormat('Y-m-d H:i:s', $published) ->toFormattedDateString(); } }
Note that the model is injected by calling the setWrappedObject
method, inherited from BasePresenter
.
We need the post class to implement the interface.
use Example\Accounts\User; use Example\Blog\PostPresenter; use McCool\LaravelAutoPresenter\HasPresenter; use Illuminate\Database\Eloquent\Model; class Post extends Model implements HasPresenter { protected $table = 'posts'; protected $fillable = ['author_id', 'title', 'content', 'published_at']; public function author() { return $this->belongsTo(User::class, 'author_id'); } public function getPresenterClass() { return PostPresenter::class; } }
Now, with no additional changes our view will show the date in the desired format.
The Decoratable
interface is used to allow the model's relations to be decorated, and the HasPresenter
interface (which extends that one) is used to have the model itself decorated.
Troubleshooting
If an object isn't being decorated correctly in the view then there's a good chance that it's simply not in existence when the view begins to render. For example, lazily-loaded relationships won't be decorated. You can fix this by eager-loading them instead. Auth::user() will never be decorated. I prefer to bind $currentUser to my views, anyway.
If an object is a relation of another object and it isn't being decorated in the view, you might not have added the Decoratable
interface to the other object. To fix this, add the Decoratable
interface to the other object.
Security
If you discover a security vulnerability within this package, please send an email to security@tidelift.com. All security vulnerabilities will be promptly addressed. You may view our full security policy here.
License
Laravel Auto Presenter is licensed under The MIT License (MIT).
For Enterprise
Available as part of the Tidelift Subscription
The maintainers of mccool/laravel-auto-presenter
and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.