Resources

Creation of a resource

This is an example of a resource, used to "map" data from controllers to the actual API response.

For more info always check the official Laravel documentation.

class UserFullResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'slug' => $this->slug,
            'name' => $this->name,
            'surname' => $this->surname,
            'email' => $this->email,
            'password' => $this->password,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'user_image' => MediaFullResource::collection($this->whenLoaded('userImage')),
            'roles' => RoleFullResource::collection($this->whenLoaded('roles')),
        ];
    }
}

In Balance there are two types of resources: the full and the minimal. The difference between these two is simply that the full one will contain all the properties of the model and the minimal one will contain only a minimum subset of those, for example id or name.

This is an example of a minimal resource.


Another type of resource frequently used in Balance is the meta resource, that will return the data of the model itself and the meta data.

Last updated