Reputation: 823
I am trying to run this code :
Route::group(["prefix" => "{user}", "middleware" => "web"], function () {
Route::get("/", function (User $user) {
try {
return $user;
} catch (\Exception $e) {
echo "ads";
}
});
Route::get("/photos", function (User $user) {
return $user->albums;
});
});
So in above code i am using Route Binding when user enter url like this : http://localhost:8000/myusername then it will show that user information but if user will not available i just want to catch exception so that is not working.
I am getting error :
What should be a issue ? Why try catch not working.
Upvotes: 0
Views: 321
Reputation: 15131
The way you are trying will not work. You have to bind
, to show Laravel how to query by name, instead of ID.
Add this before your Route::group
:
Route::bind('user', function($value)
{
return User::where('name', $value)->first();
});
You can remove your try/catch. If you want to check if your model has been found, just use like this:
Route::get("/", function ($id) {
try {
return User::findOrFail($id);
} catch(ModelNotFoundException $e) {
echo "ads"
}
});
Upvotes: 1