
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
- Always load the relationship data with eager loading in Laravel
- How to remove package from laravel
- After image selected get validation error in laravel
- Input file with max size validation in laravel
- Create record with unique slug in laravel
- How to get images from AWS s3 and display in Laravel blade
- Recursive function example code PHP Laravel
- Get posts belongs to a specific user in Laravel
- How to get user information using hootlex/laravel-friendships package in laravel
- Create project factory and seed data in laravel
- How to get only time from created_at in laravel
- Run artisan command to generate key in laravel
- PhpMyAdmin - Error The mysqli extension is missing
- Call to undefined method App\Models\User::follow()
- Skip first n record and display rest records in laravel view
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- How to fetch single row data from database in laravel
- How to send email in laravel
- Remove several global scope from query
- Call to a member function getRelationExistenceQuery() on array in Laravel
- Link storage folder in laravel 8
- How to check email is valid or not in Laravel
- Method Illuminate\Http\Request::validated does not exist
- How to get CSRF token in laravel controller
- Session Doesn't Work on Redirect