0
0
Laravelframework~20 mins

Accessing request data in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Request Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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');
    }
}
A"alice"
B"guest"
Cnull
DAn error is thrown
Attempts:
2 left
💡 Hint
The input method returns the value of the given key or the default if not present.
📝 Syntax
intermediate
2: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?
A$page = $request->param('page', 1);
B$page = $request->query('page', 1);
C$page = $request->get('page', 1);
D$page = $request->input('page', 1);
Attempts:
2 left
💡 Hint
Use the method designed specifically for query parameters.
🔧 Debug
advanced
2: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;
}
AThe 'email' field is missing from the request body.
BThe method should use $request->json('email') instead.
CThe request must be cast to an array before accessing fields.
DThe request object does not have dynamic properties for JSON fields.
Attempts:
2 left
💡 Hint
Accessing request data requires specific methods, not property access.
state_output
advanced
2: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;
}
AAn array with all request fields
B{"name": "John"} if only 'name' is sent
C{"name": "John", "age": 30} if both fields are sent
Dnull
Attempts:
2 left
💡 Hint
The only method returns only specified keys if present.
🧠 Conceptual
expert
2: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?
A$request->except('password');
B$request->only('password');
C$request->input()->except('password');
D$request->all()->except('password');
Attempts:
2 left
💡 Hint
Use the method designed to exclude keys from input data.