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:
-
Ensure Correct Middleware Order: The
SubstituteBindingsmiddleware 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. -
Check Route Definitions: Make sure that your route definitions in
routes/web/company/web.phpare 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. -
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); } -
Debugging Middleware: Add some logging in your
SetDefaultCompanyRouteParametersandResolveCompanyFromSlugmiddleware 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); } -
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 -
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.