0
0
Laravelframework~20 mins

Why APIs serve modern applications in Laravel - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Mastery in Laravel
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do modern applications use APIs?
Which of the following best explains why modern applications rely on APIs?
AAPIs are used to store all application data locally on the user's device.
BAPIs are only used to speed up database queries within a single application.
CAPIs replace the need for any user interface in applications.
DAPIs allow different software systems to communicate and share data easily.
Attempts:
2 left
💡 Hint
Think about how apps talk to each other or to servers.
component_behavior
intermediate
2: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);
});
A{"name":"John Doe","email":"john@example.com"}
BPlain text response with user name only
C500 Internal Server Error
D{"error":"User not found"}
Attempts:
2 left
💡 Hint
Check what happens if the user is found.
📝 Syntax
advanced
2: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]);
}
Aresponse()->json() cannot return arrays
BMissing semicolon after User::find($id)
CIncorrect method name 'show' should be 'ShowUser'
DUser::find() requires two arguments
Attempts:
2 left
💡 Hint
Look carefully at the end of the first line inside the method.
state_output
advanced
2: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));
A{"id":1,"name":"JOHN DOE","email":"john@example.com"}
B{"id":1,"name":"John Doe","email":"john@example.com"}
C{"id":1,"name":null,"email":"john@example.com"}
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Check how the name is transformed in toArray method.
🔧 Debug
expert
2: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']);
});
AThe response()->json() method is not allowed in POST routes
BRoute method should be GET instead of POST
CMissing CSRF token in the POST request
DThe route URL is incorrect and does not match the request
Attempts:
2 left
💡 Hint
419 errors in Laravel usually relate to security tokens.