Simplifying presenters in Laravel
In the Laravel template that we use to kickstart all our client projects at Spatie, I recently changed the way we handle presenters. Instead of using Jeffrey Way's popular presenter package we now use simple traits. In this post I want to give some background on that change.
In case you've never heard what a presenter is, let me give you a crash course. Imagine you have a User
model with a first_name
and last_name
attribute and that you need to show the full name of the user in your UI. Sure you could just code this in a Blade view.
{{ $user->first_name}} {{ $user->last_name }}
But that becomes tedious very quickly when you need to do this in several views. You could solve this by adding a fullName
function to your User
-model:
namespace App\Models;
class User
{
...
public function fullName(): string
{
return $this->first_name . ' ' . $this->last_name;
}
}
Sure, that'll work, but when I open up a model file I do not want to see methods on how things should be presented in the UI. For one small function it's probably fine, but in a real life projects there will be many of such presentation methods. Putting them all in your model file itself will make it bloated.
A common solution is to place such methods in their own dedicated class: a presenter class. In the past we've used the popular presenter package by Jeffrey Way for our presenting needs. It's a very nice package, but there are some things that were bothering me. Using the package this is the way to call a method on a presenter in a Blade view:
{{ $user->present()->fullName }}
That present()
part is kinda ugly. It would be much nicer if we could just call {{ $user->fullName }}
in our view. But we don't want to bloat our model by adding a fullName
function to it. This can be solved pragmatically by using traits. In most cases traits are used to dry up code duplication across classes. But in my mind it's perfectly fine to use traits to break down a big class in smaller bits. So in our case we could just make a separate presenter trait for each model that needs a presenter.
Using example above the UserPresenter
could look like this:
namespace App\Models\Presenters;
trait UserPresenter
{
public function getFullNameAttribute(): string
{
return $this->first_name . ' ' . $this->last_name;
}
}
namespace App\Models;
use App\Models\Presenters\UserPresenter;
class User
{
use UserPresenter;
...
}
Notice that we used an Eloquent accessor. In this way we can use {{ $user->fullName }}
in a view. The model didn't become bloated with presentable methods. And the presenters-package can be ditched. Triple win! Let me know in the comments below what you think of this approach.
What are your thoughts on "Simplifying presenters in Laravel"?