Bikram's Tech Journey

Technical notes and writings

Eloquent Route Presenter

This week Bikram Tuladhar shared route presenter for Laravel which leverages Laravel Eloquent to generate URL. Eloquent Route Presenter populates URL like $model->route->index from a model. Below is the trait which contains all the logic to generate URL.

This trait populates common restful routes (i.e. CRUD routes) by default. You can use this trait with any eloquent model and also overwrite the routes & route prefix as you need in the model class.

To overwrite and add new route use $routes variable in the model class:

$routes = ['publish' => ['routename', 'route_parameter_name']];

Or:

$routes = ['publish' => ['routename', 'id', 'relation_model->id']];

To manually specify different route prefix use $routePrefix:

$routePrefix = 'user';

Example of using eloquent route presenter in model:

Using route presenter in blade template:

Above the example, $user model object can populate URL and inject a dynamic parameter to a route by using end route name as a function call.

Below there is implementation and usages:

$user->route->view // https://app_url/user/1

$user->route->publish(['user' => $user->id, 'profile' => $user->profile->id])
// https://app_url/user/1/profile/1/publish

© 2026 · Crafted with care