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
- Get laravel version
- How to use or operator in laravel
- 419 page expired error in Laravel
- First and last item of the array using foreach iteration in laravel blade
- InRandomOrder() method with example in laravel
- How to add foreign key in laravel using migration
- How to get records in random order in laravel
- How to pass variable from controller to model in Laravel
- Retain selected value of select box in Laravel
- How to get data from two tables in laravel
- How to display validation error in laravel
- How to create belongstomany relation using custom name on custom pivot table
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- How to get database name in Laravel 9 ?
- Route [password.request] not defined
- Laravel 10 starter app using breeze on live server
- Use withCount() to Calculate Child Relationship Records
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Pagination in laravel
- Get content from web URL in laravel
- Touch parent updated_at in Laravel
- Extra Filter Query on Relationships in Laravel
- Display option of select as selected with blade directive Laravel
- Insert values in pivot table dynamically in laravel
- Pass value from controller to model in laravel