It’s not uncommon to have tens, if not hundreds of views in a Laravel application. Something that soon gets out of hand is the various references to routes. Think about how many times you’ve done something like this in your blade views:
1<a href="{{ route('users.show', $user) }}">{{ $user->name }}</a>
If for whatever reason we have to make a change to either the route alias or default query string values you’ll soon find yourself doing mass string replacements across your entire application which brings the risk of breakage within many files.
What can we do to possibly better handle this? There are a couple of different approaches.
Eloquent Only
1<?php 2 3namespace App; 4 5class User { 6 7 protected $appends = [ 8 'url' 9 ];1011 public function getUrlAttribute()12 {13 return route('users.show', $this);14 }15}
Then in your views it would look like this:
1<a href="{{ $user->url }}">{{ $user->name }}</a>
Super clean, right? For you advanced developers you’ll probably want to go with this next approach.
Eloquent with URL Presenter
At first glance, you’ll see some similarities, but the key difference is that our accessor will return an instance of the presenter.
1<?php 2 3namespace App; 4 5use App\Presenters\User\UrlPresenter; 6 7class User { 8 9 protected $appends = [10 'url'11 ];1213 public function getUrlAttribute()14 {15 return new UrlPresenter($this);16 }17}
1<?php 2 3namespace App\Presenters\User; 4 5use App\User; 6 7class UrlPresenter { 8 9 protected $user;1011 public function __construct(User $user)12 {13 $this->user = $user;14 }1516 public function __get($key)17 {18 if(method_exists($this, $key))19 {20 return $this->$key();21 }2223 return $this->$key;24 }2526 public function delete()27 {28 return route('users.delete', $this->user);29 }3031 public function edit()32 {33 return route('users.edit', $this->user);34 }3536 public function show()37 {38 return route('users.show', $this->user);39 }4041 public function update()42 {43 return route('users.update', $this->user);44 }45}
Then you would use it like this:
1<a href="{{ $user->url->show }}">{{ $user->name }}</a>
As you can see with either approach, the view now doesn’t care about how we determine the URL, just that a URL is returned. The beauty to this is should you have to adjust any routes you only have to edit two files, not hundreds.