In Laravel, you can define a route with multiple parameters by specifying them in the route definition using two curly braces ({}
) and separating them with slashes (/
). Here's an example:
Route::get('user/{id}/{name}', function ($id, $name) {
// Your code here
});
In this example, the route user/{id}/{name}
has two parameters {id}
and {name}
. The values of these parameters will be passed as arguments to the closure function defined for the route. You can then use these arguments inside the function to perform any desired logic or data retrieval.
You can also specify additional constraints on the parameters by using regular expressions. For example, you can define that the {id}
parameter should only accept numeric values using the following format:
Route::get('user/{id}/{name}', function ($id, $name) {
// Your code here
})->where('id', '[0-9]+');
In this case, the {id}
parameter should only accept numeric values.
Additionally, you can also use route model binding to automatically fetch data from your database based on one or more route parameters. This can be done by type-hinting the required model in the route closure:
use App\Models\User;
Route::get('user/{user}', function (User $user) {
// Your code here
});
In this example, the User
model will be automatically retrieved from the database based on the {user}
parameter's value. The retrieved user object is then passed as an argument to the closure function.