0
0
Laravelframework~20 mins

API resource classes in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
API Resource 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 API Resource?
Given the following Laravel API Resource class, what will be the JSON output when returning new UserResource($user) where $user has id=5, name='Alice', and email='alice@example.com'?
Laravel
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'user_id' => $this->id,
            'user_name' => $this->name,
            'contact' => $this->email,
        ];
    }
}
A{"userId":5,"userName":"Alice","emailAddress":"alice@example.com"}
B{"user_id":5,"user_name":"Alice","contact":"alice@example.com"}
C[5,"Alice","alice@example.com"]
D{"id":5,"name":"Alice","email":"alice@example.com"}
Attempts:
2 left
💡 Hint
Look at the keys returned in the toArray method and match them with the output.
lifecycle
intermediate
1:30remaining
When is the toArray method called in a Laravel API Resource?
In Laravel API Resource classes, at what point is the toArray method executed?
AImmediately when the resource object is created with <code>new</code>.
BOnly when explicitly calling <code>toArray()</code> in the controller.
CWhen the resource is converted to JSON, such as when returned from a controller.
DWhen the resource is saved to the database.
Attempts:
2 left
💡 Hint
Think about when Laravel sends data to the browser or API client.
📝 Syntax
advanced
2:30remaining
Which option correctly adds a conditional attribute in a Laravel API Resource?
You want to include the admin_notes attribute only if the user is an admin. Which code snippet inside toArray is correct?
A
return [
  'id' =&gt; $this-&gt;id,
  'name' =&gt; $this-&gt;name,
  'admin_notes' =&gt; $this-&gt;when($this-&gt;is_admin, $this-&gt;admin_notes),
];
B
return [
  'id' =&gt; $this-&gt;id,
  'name' =&gt; $this-&gt;name,
  'admin_notes' =&gt; $this-&gt;admin_notes if ($this-&gt;is_admin),
];
C
return [
  'id' =&gt; $this-&gt;id,
  'name' =&gt; $this-&gt;name,
  'admin_notes' =&gt; $this-&gt;is_admin ? $this-&gt;admin_notes : null,
];
D
return [
  'id' =&gt; $this-&gt;id,
  'name' =&gt; $this-&gt;name,
  'admin_notes' =&gt; $this-&gt;when($this-&gt;admin_notes, $this-&gt;is_admin),
];
Attempts:
2 left
💡 Hint
Use Laravel's when helper method correctly.
🔧 Debug
advanced
2:30remaining
Why does this Laravel API Resource return an empty JSON object?
Given this resource class, why does the API return {} instead of user data?
Laravel
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    public function toArray($request)
    {
        // Missing return statement
        [
            'id' => $this->id,
            'name' => $this->name,
        ];
    }
}
AThe resource class must extend <code>ResourceCollection</code> instead of <code>JsonResource</code>.
BThe resource class is missing the <code>JsonSerializable</code> interface implementation.
CThe <code>$this->id</code> and <code>$this->name</code> properties are not accessible.
DThe <code>toArray</code> method does not return the array; it just creates it without returning.
Attempts:
2 left
💡 Hint
Check if the toArray method returns the data.
🧠 Conceptual
expert
2:00remaining
What is the main benefit of using Laravel API Resource classes?
Why should you use Laravel API Resource classes instead of returning Eloquent models directly in API responses?
AThey allow you to control and customize the JSON structure and hide sensitive data easily.
BThey automatically cache all API responses for better performance.
CThey convert Eloquent models into XML format for API clients.
DThey replace the need for database migrations and schema definitions.
Attempts:
2 left
💡 Hint
Think about data control and security in API responses.