0
0
Laravelframework~10 mins

Resource collections in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return a resource collection in Laravel.

Laravel
return [1]::collection($users);
Drag options to blanks, or click blank then click option'
AUserModel
BUserCollection
CUserResource
DUserController
Attempts:
3 left
💡 Hint
Common Mistakes
Using the model class instead of the resource class.
Using a collection class that does not exist.
Calling collection on the controller.
2fill in blank
medium

Complete the code to create a resource collection class using Artisan.

Laravel
php artisan make:resource [1] --collection
Drag options to blanks, or click blank then click option'
AUserResource
BUserController
CUserModel
DUserCollection
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'UserResource' (single resource) instead of 'UserCollection'.
Omitting the --collection flag.
Using model or controller names.
3fill in blank
hard

Fix the error in the resource collection response code.

Laravel
return new [1]($users);
Drag options to blanks, or click blank then click option'
AUserModel
BUserCollection
CUserResource
DUserController
Attempts:
3 left
💡 Hint
Common Mistakes
Using the single resource class for multiple items.
Using the model or controller class.
Not wrapping the data in a resource or collection.
4fill in blank
hard

Fill both blanks to define a resource collection class extending the correct base class.

Laravel
class UserCollection extends [1]
{
    public function toArray($request)
    {
        return [2]::collection($this->collection);
    }
}
Drag options to blanks, or click blank then click option'
AIlluminate\Http\Resources\Json\ResourceCollection
BUserResource
CUserModel
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Extending the model or controller instead of ResourceCollection.
Returning the collection directly without wrapping.
Using the wrong resource class inside toArray.
5fill in blank
hard

Fill all three blanks to customize the resource collection's additional data.

Laravel
public function with($request)
{
    return [
        '[1]' => [2],
        '[3]' => 'Success'
    ];
}
Drag options to blanks, or click blank then click option'
Ameta
B['version' => '1.0']
Cstatus
Dtrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keys like 'data' or 'info' instead of 'meta'.
Setting 'meta' value as a string instead of an array.
Omitting the 'status' key or using wrong values.