I have two models that I don't want to have a prefix in front of its URLs. E.g. Users and Posts
If I have a URL https://example.com/title-of-the-post and https://example.com/username
I will do something like this in the web.php routes file:
// User
Route::get('{slug}', function ($slug) {
$user = User::whereSlug($slug)->get();
return view('users.show', $user);
});
// Post
Route::get('{slug}', function ($slug) {
$post = Post::whereSlug($slug)->get();
return view('posts.show', $user);
});
Now the issue I am facing is that the first route is entered and will never reach the second even if there is no model with a matching slug.
How can I exit to the next route (Post) if $user is not found?
Note: I have tried many different exit strategies but none seem to work.
return;
return false;
return null;
// and return nothing
Thanks in advance!
UPDATE:
Another issue is that if I have other resource routes they too get blocked by the first route.
E.g. If I have Route::resource('cars', 'CarController') it generates a /cars path which matches the {slug} and is also being blocked by first User route.