Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

morcen's avatar

Pre-populate Route Parameter

In a multi-tenant system that I am building, the company's slug is always included in the URL, and the route looks like this:

Route::group(['middleware' => ['web', 'auth'], 'prefix' => '/{company_slug}'], function () {
    Route::get('/', 'DashboardController')->name('dashboard');
    //...other routes
});

Now, I am required to provide the company slug, which is based on the logged-in user, to every route like:

// dashboard requires one parameter "company_slug"
<a href="{{ route('dashboard', auth()->user()->company->slug) }}">{{ $user->company->name }}</a>

It is starting to feel repetitive and codes not looking DRY anymore with this approach, so I'm wondering if there's a better way of doing this?

I have tried putting this in RouteServiceProvider:

    public function boot()
    {
        //
        parent::boot();

        Route::model('company', Company::class);
    }

and this at Company model:

    public function resolveRouteBinding($value)
    {
        return auth()->user()->company; // also tried using auth()->user()->company->slug
    }

and then I removed the company parameter in the route usage, but I'm getting the Missing required parameters error so I must be doing it incorrectly.

My desired outcome is that I can use routes without the need to put the company's slug all the time, like:

// dashboard requires one parameter "company_slug"
<a href="{{ route('dashboard') }}">{{ $user->company->name }}</a>

Can I get an expert opinion on this or point me what am I doing wrong? The company slug in the URL is required by the client, and is being used as an identifier to some API stuff, so it's not an option to remove it.

0 likes
3 replies
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

@morcen something like this might work:

php artisan make:middleware SetDefaultCompanySlugForRoutes

public function handle($request, Closure $next)
{
    $route = app('router')->getCurrentRoute();

    if($route->hasParameter('company_slug')) {
        app('url')->defaults([
            'company_slug' => $route->parameter('company_slug'),
        ]);
    }
    
    return $next($request);
}
use App\Http\Middleware\SetDefaultCompanySlugForRoutes;

//...

Route::group([
   'middleware' => [
      'web',
      'auth',
      SetDefaultCompanySlugForRoutes::class
   ],
   'prefix' => '/{company_slug}'
], function () {
    //...
});

https://github.com/laravel/framework/blob/7.x/src/Illuminate/Routing/UrlGenerator.php#L596

morcen's avatar

Thank you @sti3bas , I will try it now and let you know of the result

morcen's avatar

Works like a charm mate, thank you

Please or to participate in this conversation.