
Laravel route parameter
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; });
0Visit 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; });
0Visit 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.
Random Code Snippet Queries: Laravel
- Get only 10 records from table in laravel
- How to insert ckeditor data into database in Laravel?
- Link storage folder in laravel 8
- How to create and run user seeder in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1055
- How to check email is valid or not in Laravel
- How to get last month records in Laravel
- Get id of last inserted record in laravel
- The openssl extension is required for SSL/TLS protection but is not available
- Display message with session flash using bootstrap alert class in laravel
- External link not working in laravel blade
- Symlink(): No such file or directory
- How to pass data to route in laravel?
- Ajax GET request in laravel
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- Laravel save object to database
- How to update record after save method in Laravel
- Validation for multiple forms on same page in laravel
- Conditional where clause in Laravel
- How to validate URL with https using regex in laravel
- Add a subselect based on relationship using withAggregate method
- Laravel get all records with pagination
- Route [password.request] not defined
- SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'user_id'
- How to get all route list