Bird
0
0

You want to safely get a nested input value 'user.address.city' from a Laravel request and provide a default 'Unknown' if it does not exist. Which code snippet correctly does this?

hard📝 Application Q15 of 15
Laravel - Request and Response
You want to safely get a nested input value 'user.address.city' from a Laravel request and provide a default 'Unknown' if it does not exist. Which code snippet correctly does this?
A$city = $request->input('user.address.city', 'Unknown');
B$city = $request->get('user')['address']['city'] ?? 'Unknown';
C$city = $request->query('user.address.city') ?: 'Unknown';
D$city = $request->input('user')['address']['city'] ?? 'Unknown';
Step-by-Step Solution
Solution:
  1. Step 1: Use dot notation with input()

    Laravel supports dot notation in input() to access nested data safely with a default value.
  2. Step 2: Analyze other options

    B uses valid get() but unsafe array access if 'user' missing, C uses query() which only reads URL params, D uses input('user') which may be null leading to array access error.
  3. Final Answer:

    $city = $request->input('user.address.city', 'Unknown'); -> Option A
  4. Quick Check:

    Use input('nested.key', default) for nested safe access [OK]
Quick Trick: Use input('nested.key', default) for nested values [OK]
Common Mistakes:
  • Trying to access nested arrays without checking existence
  • Unsafe manual array access after get('user') or input('user')
  • Using query() which only reads URL parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes