Challenge - 5 Problems
Request Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Laravel controller method?
Consider this Laravel controller method that handles a POST request with form data. What will be returned when the form sends a field named
username with value alice?Laravel
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { public function store(Request $request) { return $request->input('username', 'guest'); } }
Attempts:
2 left
💡 Hint
The input method returns the value of the given key or the default if not present.
✗ Incorrect
The
input method fetches the value of 'username' from the request data. Since the form sends 'alice', it returns 'alice'. The default 'guest' is ignored.📝 Syntax
intermediate2:00remaining
Which option correctly retrieves a query parameter named 'page' from the request?
You want to get the 'page' parameter from the URL query string in a Laravel controller. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Use the method designed specifically for query parameters.
✗ Incorrect
The
query method fetches parameters specifically from the URL query string. input and get (an alias for input) fetch from all input sources (query string, form data, JSON, etc.), making query more precise here. param is not a valid method on the Request object.🔧 Debug
advanced2:00remaining
Why does this code cause an error when accessing JSON request data?
This Laravel controller method tries to get a JSON field 'email' from the request body. Why does it cause an error?
Laravel
<?php
public function update(Request $request)
{
$email = $request->email;
return $email;
}
Attempts:
2 left
💡 Hint
Accessing request data requires specific methods, not property access.
✗ Incorrect
Laravel's Request object does not support accessing input data as properties. You must use methods like
input or json to get data from the request.❓ state_output
advanced2:00remaining
What is the value of $data after this code runs?
Given this Laravel controller snippet, what will be the value of
$data?Laravel
<?php
public function handle(Request $request)
{
$data = $request->only(['name', 'age']);
return $data;
}
Attempts:
2 left
💡 Hint
The only method returns only specified keys if present.
✗ Incorrect
The
only method returns an array with only the specified keys if they exist in the request. If both 'name' and 'age' are sent, both appear in the array.🧠 Conceptual
expert2:00remaining
Which method correctly retrieves all input data except the 'password' field?
You want to get all input data from a Laravel request except the 'password' field. Which method call achieves this?
Attempts:
2 left
💡 Hint
Use the method designed to exclude keys from input data.
✗ Incorrect
The
except method returns all input data except the specified keys. only returns only those keys. input() returns a value, not a collection, so chaining except fails. all() returns an array, which does not have an except method.