Laravel Tutorials

Leverage Eloquent To Prepare Your URLs

Published
Leverage Eloquent To Prepare Your URLs image

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:

<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

<?php

namespace App;

class User {

  protected $appends = [
    'url'
  ];

  public function getUrlAttribute()
  {
    return route('users.show', $this);
  }
}

Then in your views it would look like this:

<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.

<?php

namespace App;

use App\Presenters\User\UrlPresenter;

class User {

  protected $appends = [
    'url'
  ];

  public function getUrlAttribute()
  {
    return new UrlPresenter($this);
  }
}

<?php

namespace App\Presenters\User;

use App\User;

class UrlPresenter {

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function __get($key)
    {
        if(method_exists($this, $key))
        {
            return $this->$key();
        }

        return $this->$key;
    }

    public function delete()
    {
        return route('users.delete', $this->user);
    }

    public function edit()
    {
        return route('users.edit', $this->user);
    }

    public function show()
    {
        return route('users.show', $this->user);
    }

    public function update()
    {
        return route('users.update', $this->user);
    }
}

Then you would use it like this:

<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.

Jordan Dalton photo

Founder of Larastream.com and Laracrunch.com

Sponsored

acquaintsoft logo
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech

The latest

View all →
Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures image

Laravel AI SDK 1.4 Lets Agents Recover From Tool Failures

Read article
Laravel Read Replicas: Check if a Replica Has Caught Up image

Laravel Read Replicas: Check if a Replica Has Caught Up

Read article
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article