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

drewclem's avatar

App loses route model binding w/ subdomain routing [Laravel 11]

I've been trying to setup subdomains in a Laravel 11 app. The routing works and the app loads. However, any route using route/model binding receives the error below. It happens for any route/model bound route, not just this one.

// example error:
App\JobListings\Http\Controllers\Company\ShowJobListingCandidatesController::__invoke(): Argument #2 ($jobListing) must be of type App\Models\JobListing, string given

This is my route configuration, and I've tried setting middleware priority as the docs (and numerous threads) have mentioned to do.

I'm setting URL::defaults in the SetDefaultCompanyRouteParameters like so:

// SetDefaultCompanyRouteParameters
public function handle(Request $request, Closure $next): Response
    {
        if ($request->attributes->get('currentCompany')) {
            // https://github.com/tighten/ziggy?tab=readme-ov-file#default-parameter-values
            URL::defaults(['slug' => $request->attributes->get('currentCompany')->slug]);
        }

        return $next($request);
    }

And resolving company slugs like so:

ResolveCompanyFromSlug

public function handle(Request $request, Closure $next): Response
    {
        $slug = $request->route('slug');

        $company = Company::where('slug', $slug)->first();

        // TODO: Determine how to handle this case.
        if (! $company) {
            abort(404, 'Company not found.');
        }

        $request->attributes->add(['currentCompany' => $company]);

        return $next($request);
    }

I've tried different sequencing of the middleware, tinkered with the priority order, and have tried a handful of other things I've sen mentioned in other articles to no avail.

Has anyone ran into this before and know what the fix is? It's specific to something with subdomains _somewhere, because if I revert to a commit before subdomains then app runs just fine (albeit without subdomains).

0 likes
4 replies
LaryAI's avatar
Level 58

The issue you're encountering with route model binding in a subdomain setup is likely related to the order of middleware execution and how the route parameters are being resolved. Here are a few steps you can take to troubleshoot and potentially resolve the issue:

  1. Ensure Correct Middleware Order: The SubstituteBindings middleware should be executed after your custom middleware that resolves the company from the slug. This ensures that the route model binding has access to the correct parameters. You seem to have set this in the middleware priority, but double-check that it's being applied correctly.

  2. Check Route Definitions: Make sure that your route definitions in routes/web/company/web.php are correctly set up to use route model binding. For example, if you have a route like this:

    Route::get('/job-listings/{jobListing}', [ShowJobListingCandidatesController::class, '__invoke']);
    

    Ensure that the {jobListing} parameter is correctly defined in your route model binding.

  3. Explicit Route Model Binding: If the automatic route model binding is not working as expected, you can explicitly define it in the RouteServiceProvider:

    use App\Models\JobListing;
    
    public function boot()
    {
        parent::boot();
    
        Route::model('jobListing', JobListing::class);
    }
    
  4. Debugging Middleware: Add some logging in your SetDefaultCompanyRouteParameters and ResolveCompanyFromSlug middleware to ensure that they are being executed and that the parameters are being set correctly. For example:

    public function handle(Request $request, Closure $next): Response
    {
        $slug = $request->route('slug');
        \Log::info('Slug:', ['slug' => $slug]);
    
        $company = Company::where('slug', $slug)->first();
        if (!$company) {
            abort(404, 'Company not found.');
        }
    
        $request->attributes->add(['currentCompany' => $company]);
        \Log::info('Current Company:', ['company' => $company]);
    
        return $next($request);
    }
    
  5. Check Route Caching: If you are using route caching (php artisan route:cache), clear the cache to ensure that your changes are being applied:

    php artisan route:clear
    
  6. Verify Subdomain Configuration: Double-check your subdomain configuration in your local environment and ensure that the subdomains are correctly pointing to your Laravel application.

By following these steps, you should be able to identify where the issue lies and resolve the problem with route model binding in your subdomain setup. If the problem persists, consider isolating the issue by creating a minimal example to test the subdomain and route model binding separately.

drewclem's avatar
drewclem
OP
Best Answer
Level 1

If anyone comes across this, the answer is adding in your middleware

$request->route()->forgetParameter('company_slug');

In my case, I added it to the ResolveCompanyFromSlug middleware

Please or to participate in this conversation.