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.

class TaskMinimalResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'index' => $this->index,
            'slug' => $this->slug,
            'title' => $this->title,
            'attachments' => $this->whenLoaded('attachments'),
        ];
    }
}

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.

class TaskMetaResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'data' => array_key_exists('data', $this->resource) ?
                (array_key_exists('data', $this['data']->toArray())
                    ? new BasePaginatedCollectionResource($this['data'], TaskFullResource::class)
                    : new TaskFullResource($this['data'])
                )
                : null,
            'meta' => array_key_exists('meta', $this->resource) ?
                [
                    'projects' => array_key_exists('projects', $this['meta']) ? ProjectMinimalResource::collection($this['meta']['projects']) : null,
                    'orders' => array_key_exists('orders', $this['meta']) ? OrderMinimalResource::collection($this['meta']['orders']) : null,
                    'tasks' => array_key_exists('tasks', $this['meta']) ? TaskMinimalResource::collection($this['meta']['tasks']) : null,
                    'tags' => array_key_exists('tags', $this['meta']) ? TagResource::collection($this['meta']['tags']) : null,
                    'users' => array_key_exists('users', $this['meta']) ? UserMinimalResource::collection($this['meta']['users']) : null,
                    'priority' => array_key_exists('priority', $this['meta']) ? [
                        [
                            'value' => $this['meta']['priority'][0]['value'],
                        ],
                        [
                            'value' => $this['meta']['priority'][1]['value'],
                        ],
                        [
                            'value' => $this['meta']['priority'][2]['value'],
                        ],
                        [
                            'value' => $this['meta']['priority'][3]['value'],
                        ],
                        [
                            'value' => $this['meta']['priority'][4]['value'],
                        ],
                    ] : null
                ] : null,
        ];
    }
}

Last updated