Skip to content

Route parameters in TenantFinder #455

Closed Answered by yitznewton
yitznewton asked this question in Q&A
Discussion options

You must be logged in to vote

I got this to work by allowing my TenantFinder to return null when $request->route() is empty, then re-attempting the findForRequest() in a route middleware:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Spatie\Multitenancy\TenantFinder\TenantFinder;

readonly class SetCurrentTenant
{
    public function __construct(private Application $app)
    {
    }

    public function handle(Request $request, \Closure $next)
    {
        $tenant = $this->app[TenantFinder::class]->findForRequest($request);
        $tenant?->makeCurrent();

        return $next($request);
    }
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by yitznewton
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant