This example shows a UserResource that formats each user with id, name, and email. The controller returns all users wrapped in this resource collection, so the API response is a clean list of users.
Laravel
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class UserResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
];
}
}
// In a controller method:
use App\Models\User;
use App\Http\Resources\UserResource;
public function index()
{
$users = User::all();
return UserResource::collection($users);
}
OutputSuccess
Important Notes
Resource collections automatically wrap each item using the resource's toArray method.
You can add extra data to the whole collection using the additional() method.
Resource collections help keep your API responses consistent and easy to change later.
Summary
Resource collections format many items in the same way for API responses.
Use ::collection() on a resource class to wrap multiple models.
You can add extra info to collections with additional().
Practice
(1/5)
1. What is the primary purpose of using ::collection() in Laravel resource classes?
easy
A. To delete multiple records from the database
B. To create a single model instance
C. To format multiple model instances consistently in API responses
D. To validate incoming request data
Solution
Step 1: Understand the role of resource collections
Resource collections are used to format many items in the same way for API responses.
Step 2: Identify what ::collection() does
The ::collection() method wraps multiple model instances to apply the resource formatting to each item.
Final Answer:
To format multiple model instances consistently in API responses -> Option C
Quick Check:
Resource collections format many items = A [OK]
Hint: Remember: ::collection() wraps many models for consistent output [OK]
Common Mistakes:
Confusing ::collection() with single model resource
Thinking it deletes or modifies database records
Mixing it up with validation methods
2. Which of the following is the correct syntax to return a resource collection for a list of users stored in $users using a resource class UserResource?
easy
A. return UserResource::collection($users);
B. return new UserResource($users);
C. return UserResource::make($users);
D. return UserResource::all($users);
Solution
Step 1: Recall the syntax for resource collections
To format multiple models, use ::collection() on the resource class with the collection as argument.
Step 2: Match the syntax with options
return UserResource::collection($users); uses UserResource::collection($users); which is correct. Other options either create a single resource or use invalid methods.
Final Answer:
return UserResource::collection($users); -> Option A
Quick Check:
Correct syntax for collections = D [OK]
Hint: Use ::collection() for multiple items, not new [OK]
Common Mistakes:
Using new UserResource($users) for collections
Using non-existent methods like all() or make()
Confusing single resource and collection syntax
3. Given the following code, what will be the output structure of the API response?
A. Missing the collection argument inside collection()
B. Using additional() is not allowed on collections
C. Should use new UserResource() instead of collection()
D. The additional() method requires a string, not an array
Solution
Step 1: Check the usage of collection()
The collection() method requires a collection or array argument to wrap. Here it is called without any argument.
Step 2: Validate other method usages
additional() can be used on collections and accepts an array. Using new UserResource() is for single models, not collections.
Final Answer:
Missing the collection argument inside collection() -> Option A
Quick Check:
collection() needs argument [OK]
Hint: Always pass your data to collection() [OK]
Common Mistakes:
Calling collection() without data
Thinking additional() is invalid on collections
Confusing single resource and collection usage
5. You want to return a paginated list of posts with a resource collection and include extra pagination info under a meta key. Which code snippet correctly achieves this?
hard
A. return PostResource::collection($posts->paginate(10))->additional(['meta' => ['current_page' => $posts->currentPage()]]);
B. return PostResource::collection($posts->paginate(10))->additional(['meta' => ['current_page' => $posts->paginate(10)->currentPage()]]);
C. return PostResource::collection($posts)->additional(['meta' => ['current_page' => $posts->currentPage()]]);
D. return new PostResource($posts->paginate(10))->additional(['meta' => ['current_page' => $posts->paginate(10)->currentPage()]]);
Solution
Step 1: Understand pagination and resource collections
When paginating, call paginate(10) on the query to get a paginator instance. Pass this to collection().
Step 2: Access pagination info correctly
To access currentPage(), it must be called on the paginator returned by paginate(), not on the original query builder $posts.
Step 3: Identify correct option
The snippet that calls $posts->paginate(10)->currentPage() within additional() correctly retrieves the current page from a paginator instance, ensuring the paginated data and meta align.
Final Answer:
return PostResource::collection($posts->paginate(10))->additional(['meta' => ['current_page' => $posts->paginate(10)->currentPage()]]); -> Option B
Quick Check:
paginate() result for both collection and currentPage() [OK]
Hint: Access pagination methods on paginate() result, not query builder [OK]
Common Mistakes:
Calling currentPage() on query builder $posts
Passing unpaginated $posts to collection()
Using new Resource() for paginated collections
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.