0
0
Laravelframework~20 mins

Resource collections in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Laravel Resource Collection 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 resource collection?
Given the following Laravel resource collection code, what will be the JSON output when returning UserResource::collection($users) where $users contains two users with names 'Alice' and 'Bob'?
Laravel
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'username' => $this->name,
            'email' => $this->email
        ];
    }
}

// Controller method
public function index()
{
    $users = User::all();
    return UserResource::collection($users);
}
A{"data":[{"username":"Alice","email":"alice@example.com"},{"username":"Bob","email":"bob@example.com"}]}
B[{"username":"Alice","email":"alice@example.com"},{"username":"Bob","email":"bob@example.com"}]
C{"users":[{"name":"Alice","email":"alice@example.com"},{"name":"Bob","email":"bob@example.com"}]}
D{"data":{"username":"Alice","email":"alice@example.com"}}
Attempts:
2 left
💡 Hint
Remember that Laravel resource collections wrap the array of resources inside a 'data' key by default.
📝 Syntax
intermediate
1:30remaining
Which option causes a syntax error in Laravel resource collection usage?
Identify which code snippet will cause a syntax error when returning a resource collection in Laravel.
Areturn new UserResource::collection($users);
Breturn UserResource::collection($users)->response();
Creturn UserResource::collection($users);
Dreturn UserResource::collection($users)->toResponse($request);
Attempts:
2 left
💡 Hint
Check how the 'new' keyword is used with static methods.
state_output
advanced
2:00remaining
What is the value of the 'meta' key in this resource collection response?
Consider this Laravel resource collection with added meta data:
return UserResource::collection($users)->additional(['meta' => ['total_users' => $users->count()]]);
What will be the value of the 'meta' key in the JSON response?
A[{"total_users":2}]
B{"total_users":2}
C{"count":2}
Dnull
Attempts:
2 left
💡 Hint
The additional method adds extra data at the root level of the JSON response.
🔧 Debug
advanced
2:30remaining
Why does this resource collection return an empty 'data' array?
Given this controller code:
public function index() {
    $users = User::where('active', true)->get();
    return UserResource::collection($users);
}
But the JSON response shows {"data":[]} even though there are active users in the database. What is the most likely cause?
AThe UserResource's toArray method returns an empty array.
BThe User model does not have an 'active' attribute.
CThe controller forgot to import the UserResource class.
DThe $users variable is empty because the query is incorrect.
Attempts:
2 left
💡 Hint
Consider why the query might not return the expected users.
🧠 Conceptual
expert
3:00remaining
Which statement about Laravel resource collections is TRUE?
Select the correct statement about Laravel resource collections.
AResource collections automatically paginate the data without extra code.
BResource collections require manual looping over each model to transform data.
CResource collections wrap the transformed data inside a 'data' key by default.
DResource collections cannot add extra meta data to the response.
Attempts:
2 left
💡 Hint
Think about the default JSON structure of resource collections.