
Laravel route parameter
Laravel route parameter
In this tutorial, we will try to find the solution to "Laravel route parameter" through programming.
Sometimes, you have to capture segments of the URI from the URL. You may do so by defining route parameters. You can pass parameters in the Laravel route in two different ways. First we can pass parameters as required and second we can pass them as optional in Laravel.-
Laravel route with required parameter
--PATH routes\web.phpRoute::get('/user/{id}', function ($id) { return 'User '.$id; });
Visit http://localhost:8000/user/1 to get the output. Here we create a route with the required parameter (id) which is returned by the callback function as the output. It will return the error if we did not pass the parameter value while visiting the URL.
-
Laravel route with optional parameter
--PATH routes\web.phpRoute::get('/user/{name?}', function ($name = 'John') { return $name; });
Visit http://localhost:8000/user/samar to get the output. Here we create a route with the optional parameter (name) which is returned by the callback function if we pass it as a URI segment else it will return john as output which is default value.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Drop foreign key column in Laravel using migration
- If condition in Laravel 9
- Get duplicate records in laravel
- Define variable and use in Laravel controller method
- Display success message in laravel
- Get all users except the followings users in overtrue laravel-follow
- Calculate age from date of birth in Laravel
- Create model with migration and seeder
- How to display validation error in laravel
- How to start websocket server in laravel
- Call to a member function pluck() on null
- Validation errors for multiple forms on same page Laravel
- How to get route name on visit URL in laravel
- Class 'App\Rules\Hash' not found in Laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Get ids in array from users table
- PhpMyAdmin - Error The mysqli extension is missing
- Delete records with relationship in laravel
- How to pass two variables in HREF in laravel
- Illuminate\Database\QueryException could not find driver
- How to check query string exists or not in laravel blade
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- Link storage folder in laravel 8
- There are no commands defined in the "route:" namespace
- Submit form without CSRF token in Laravel