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 prevent host header attack in Laravel
- Display option of select as selected with blade directive Laravel
- How to Access Array in blade laravel
- How to create static page in Laravel
- How to upload local Laravel project to server ?
- How to get path from current URL in Laravel
- How to fill a column automatically while creating records in Laravel
- How to run a specific seeder class in laravel
- The use statement with non-compound name 'DB' has no effect
- Drop foreign key column in Laravel using migration
- Laravel create default admin user
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- Insert data with form validation using ajax in laravel
- Composer\Exception\NoSslException
- Laravel get all records with pagination
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- Create project table with model and migration
- Laravel csrf token mismatch for ajax POST Request
- How to insert dynamic value to additional column in pivot table in laravel
- The use statement with non-compound name 'Auth' has no effect
- Get count of filter data, while return a small set of records
- How to pass two variables in HREF in laravel
- Laravel 5.4 save data to database
- How to validate form input data in laravel
- Post model with title and body in laravel 8