Bird
0
0

Find the bug in this Laravel code:

medium📝 Debug Q7 of 15
Laravel - Request and Response
Find the bug in this Laravel code:
return response('Hi')->cookie('token', 'abc123');

Why might this code fail to set the cookie?
AMissing duration parameter causes cookie not to be set
BCookie name 'token' is reserved and cannot be used
Cresponse()->cookie() requires CookieJar instance, not parameters
DNo bug; cookie will be set with default duration
Step-by-Step Solution
Solution:
  1. Step 1: Check response()->cookie() parameters

    The method signature is cookie($name, $value, $minutes = 0, $path = '/', ...). Duration defaults to 0 (session cookie).
  2. Step 2: Confirm behavior

    Providing only name and value sets a session cookie that expires when the browser closes. No error occurs.
  3. Final Answer:

    No bug; cookie will be set with default duration -> Option D
  4. Quick Check:

    response()->cookie defaults to session cookie [OK]
Quick Trick: response()->cookie('name', 'value') sets session cookie [OK]
Common Mistakes:
  • Thinking cookie name 'token' is reserved
  • Believing response()->cookie() requires CookieJar instance not parameters
  • Assuming missing duration prevents cookie from being set

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes