Challenge - 5 Problems
API Mastery in Laravel
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why do modern applications use APIs?
Which of the following best explains why modern applications rely on APIs?
Attempts:
2 left
💡 Hint
Think about how apps talk to each other or to servers.
✗ Incorrect
APIs act like bridges that let different software parts exchange information smoothly. This is why modern apps use them to connect with servers, databases, or other services.
❓ component_behavior
intermediate2:00remaining
Laravel API Route Behavior
Given this Laravel API route, what will be the JSON response when accessing /api/user/5 if the user exists?
Laravel
<?php use Illuminate\Support\Facades\Route; use App\Models\User; Route::get('/user/{id}', function ($id) { $user = User::find($id); if ($user) { return response()->json(['name' => $user->name, 'email' => $user->email]); } return response()->json(['error' => 'User not found'], 404); });
Attempts:
2 left
💡 Hint
Check what happens if the user is found.
✗ Incorrect
The route returns a JSON response with the user's name and email if the user exists. Otherwise, it returns a 404 error in JSON.
📝 Syntax
advanced2:00remaining
Identify the syntax error in this Laravel API controller method
What is the syntax error in this Laravel controller method for returning JSON data?
Laravel
public function show($id) {
$user = User::find($id);
return response()->json(['user' => $user]);
}Attempts:
2 left
💡 Hint
Look carefully at the end of the first line inside the method.
✗ Incorrect
In PHP, each statement must end with a semicolon. The line with User::find($id) is missing it, causing a syntax error.
❓ state_output
advanced2:00remaining
What is the output of this Laravel API resource response?
Consider this Laravel API resource response code. What JSON output will it produce?
Laravel
<?php use Illuminate\Http\Resources\Json\JsonResource; class UserResource extends JsonResource { public function toArray($request) { return [ 'id' => $this->id, 'name' => strtoupper($this->name), 'email' => $this->email ]; } } // In controller: return new UserResource(User::find(1));
Attempts:
2 left
💡 Hint
Check how the name is transformed in toArray method.
✗ Incorrect
The toArray method converts the user's name to uppercase before returning it in JSON.
🔧 Debug
expert2:00remaining
Why does this Laravel API call return a 419 error?
This Laravel API POST request returns a 419 error. What is the most likely cause?
Laravel
<?php Route::post('/submit', function () { return response()->json(['message' => 'Success']); });
Attempts:
2 left
💡 Hint
419 errors in Laravel usually relate to security tokens.
✗ Incorrect
Laravel protects POST routes with CSRF tokens. If the token is missing or invalid, Laravel returns a 419 error.