API builder

Include

You define the possible includes in the transformer.

    /** @var array $includes */
    protected $includes = [
        'farm' => FarmTransformer::class,
        'productions' => ProductionTransformer::class,
        'blockData' => BlockDataTransformer::class,
        'qrCode' => QrCodeTransformer::class,
        'createdByUser' => UserTransformer::class,
    ];

Filter

Filters are set in the Model and in the Controller.

    /** @var array $filters */
    protected static $filters = [
        'farm_id',
        'code',
        'user.name', -> filter a related attribute
    ];

Possible operators:

  • Where
  • Between
  • Equals
  • Greater
  • GreaterOrEqual
  • In
  • Less
  • LessOrEqual
  • Like
  • NotBetween
  • NotEquals
  • NotIn
  • NotLike

Example

    "_meta": {
        "filter": {
            "key.key": {
                "value": "harvest_planning_change_reason",
                "operator": "equals"
            }
        }
    }

Sort

You can sort on every attribute or related attribute.

Search

You define search attributes in your Model.

    /** @var array $searchColumns */
    protected static $searchColumns = [
        'code',
    ];

Example

    "_meta": {
        "search": searchString,
    }

Pagination

All our index request are paginated. To show all items in the database increase the perPage attribute to 9999999999.

List

Not default enabled you need to create a route and implement the list controller. Default list fields are id => name.

When you want different fields you need to extend the list method in your repository.

Example

    /**
     * @param string $label
     * @param string $key
     * @return Collection
     */
    public function list($label = 'code', $key = 'id'): Collection
    {
        return parent::list($label, $key);
    }

Example

An example request with all possible features.

 

    "_meta": {
        "include": [
            "translations",
            "key"
        ],
        "filter": {
            "key.key": {
                "value": "harvest_planning_change_reason",
                "operator": "equals"
            }
                        "key.key": {
                            "value": "harvest_planning_change_reason",
                            "operator": "equals"
                        }
        },
        "search": searchString,
        "sort": {
            "0": {
                "value": "desc"
            }
        },
        "perPage": 20,
        "page": 1
    }