Resources

Exposed Data

Fields

Each Rest resource contains a fields method. This method returns an array of strings specifying the columns you want to expose. This fields are accessible in terms of selecting and mutating.

To add a field to a resource, you may simply add it to the resource's fields method.

public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
    return [
        'id',
        'name',
    ];
}

Conditional field exposing

You might want to choose the fields you are exposing depending on the user. You can achieve this by conditioning the fields method:

public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
    $fields = [
        'id',
        'name'
    ];

    if ($request->user()->isAdministrator()) {
        array_push($fields, 'password');
    }

    return $fields;
}

Sorts

The fields allowed are the one you define in the fields method

Default sort

By default, Rest Api sorts by id descending, if you want to change this, extend the defaultOrderBy method.

/**
 * Return the default ordering for resource queries.
 *
 * @param RestRequest $request
 *
 * @return array
 */
public function defaultOrderBy(RestRequest $request): array
{
    return [
        'id' => 'desc',
    ];
}

Scopes

Each Rest resource contains a scopes method. This method returns an array of strings specifying the scopes you want to expose.

To add a scope to a resource, you may simply add it to the resource's scopes method.

    public function scopes(\Lomkit\Rest\Http\Requests\RestRequest $request)
    {
        return [
            'withTrashed'
        ];
    }

Conditional scope exposing

You might want to choose the scopes you are exposing depending on the user. You can achieve this by conditioning the scopes method:

public function scopes(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
    $scopes = [
        'withTrashed'
    ];

    if ($request->user()->isAdministrator()) {
        array_push($scopes, 'numbered');
    }

    return $scopes;
}

Limits

Each Rest resource contains a limits method. This method returns an array of numbers specifying the limits you want to allow.

To add a limit to a resource, you may simply add it to the resource's limits method.

    public function limits(\Lomkit\Rest\Http\Requests\RestRequest $request) {
        return [
            10,
            25,
            50
        ];
    }

Conditional limits specifying

You might want to choose the limits you are exposing depending on the user. You can achieve this by conditioning the limits method:

public function limits(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
    $limits = [
            10,
            25,
            50
        ];

    if ($request->user()->isAdministrator()) {
        array_push($limits, 1000);
    }

    return $limits;
}

Copyright © 2024