To make a route parameter optional in Laravel, you can use the ?
symbol at the end of the parameter name in the route definition. Here's an example:
Route::get('user/{id?}', function ($id = null) {
// If the id is not provided, it will be set to null.
// Your code logic here...
});
In this example, the id
parameter is optional. If it is not provided in the URL, it will default to null
. This allows you to make certain parts of the route optional, so you can have URLs like "/user" and "/user/1" where the "1" is the optional parameter.