# 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.

```php
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:

```php
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.

```php
/**
 * 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.

```php
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:

```php
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.

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

If no limit is specified in the request, Rest Api paginates with 50 items. You can also set a default limit by specifying it in your resource:

```php
public int $defaultLimit = 20;
```

### 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:

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

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

    return $limits;
}
```
