0
0
Laravelframework~10 mins

API resource classes 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 create a basic API resource class in Laravel.

Laravel
use Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->[1],
            'email' => $this->email,
        ];
    }
}
Drag options to blanks, or click blank then click option'
Aname
Busername
Cuser
Dfullname
Attempts:
3 left
💡 Hint
Common Mistakes
Using an attribute that does not exist on the User model.
Confusing the attribute with username or fullname.
2fill in blank
medium

Complete the code to return a collection of users using the resource class.

Laravel
use App\Http\Resources\UserResource;
use App\Models\User;

public function index()
{
    $users = User::all();
    return UserResource::[1]($users);
}
Drag options to blanks, or click blank then click option'
Amake
Bresource
Ccollection
Dall
Attempts:
3 left
💡 Hint
Common Mistakes
Using make which is for single models.
Using all which is a query method, not a resource method.
3fill in blank
hard

Fix the error in the resource class to correctly include a custom attribute.

Laravel
class UserResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'is_admin' => $this->[1],
        ];
    }
}
Drag options to blanks, or click blank then click option'
Aadmin
BisAdmin
Cadmin_status
Dis_admin
Attempts:
3 left
💡 Hint
Common Mistakes
Using camelCase attribute names that don't exist on the model.
Using attribute names that are not defined in the model.
4fill in blank
hard

Fill both blanks to add a conditional attribute to the resource output.

Laravel
class UserResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'email' => $this->email,
            'phone' => $this->when($this->[1], $this->[2]),
        ];
    }
}
Drag options to blanks, or click blank then click option'
Aphone !== null
Bphone
Cphone_number
Dphone != ''
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong attribute name for the phone number.
Not providing a proper condition for the when method.
5fill in blank
hard

Fill all three blanks to customize the resource response with additional data and a custom key.

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

    public function with($request)
    {
        return [
            '[3]' => 'User data retrieved successfully',
        ];
    }
}
Drag options to blanks, or click blank then click option'
Ausername
Brole
Cmeta
Duser_name
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keys that don't match the data.
Not using the whenLoaded method properly.