
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
- How to authenticate admin users in Laravel ?
- How to include header file in laravel
- Route prefix with auth middleware in laravel
- How to pass two variables in HREF in laravel
- Non-static method App\Http\Helper::myFunction() should not be called statically
- How to avoid duplicate entries in pivot table in Laravel
- How to call Laravel route in jQuery
- Laravel pagination links with query string
- Route group with URI prefix using middleware and route name prefixes
- How to upload multiple images after preview in laravel using cropper js
- Target class [admin] does not exist.
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Laravel form request validation
- Best code example for create order in Laravel for ecommerce
- Create project table with model and migration
- Get last record from table in laravel
- Always load the relationship data with eager loading in Laravel
- How to display validation error in laravel
- Extract only time from datetime in laravel
- Laravel change date format
- Laravel 10 starter app using breeze on live server
- Target class [HomeController] does not exist
- Display first n record from collection in laravel view
- How to get database name in Laravel 9 ?
- Get domain name in laravel