Tuner Docs

A Laravel package to fine-tune your APIs.

View on GitHub
๐Ÿ“š Documentation
๐Ÿงช Basic Features
๐Ÿงช Intermediate Features
๐Ÿงช Advanced Features

Expansion

The Expansion feature in Tuner allows API consumers to include related models directly in the response.
This removes the need for multiple requests and gives clients more control over the data they need.



Supported Relationships

Tuner supports all common Eloquent relationship types:



Setup

By default, expansion is disabled.
To enable it, you need to define the getExpandableRelations method in your model and provide relation settings.

Example

class User extends Model
{
    public function phone() // <-- relation name
    {
        return $this->hasOne(Phone::class);
    }

    protected function getExpandableRelations(): array
    {
        return [
            'phone' => [ // <-- relation name
                'options' => [
                    'projectable_columns' => ['*'],
                    'sortable_columns'   => ['*'],
                    'searchable_columns' => ['*'],
                    'filterable_columns' => ['*'],
                ],

                'table' => 'phones',  // optional
                'fk'    => 'user_id', // optional
            ],
        ];
    }
}

Explanation:



Usage

You can use the expand modifier to load related resources.
When expanding, you can assign an alias to the relation.
This alias is required if you want to apply other modifiers such as columns, sort, or filter.

Example

GET /api/users?expand[posts]=p&p_columns=id,title

Explanation:

The response will only include the id and title fields from the expanded posts relation.



Combining with Modifiers

Expansion works seamlessly with other Tuner modifiers:

Sort (sort)

GET /api/users?expand[posts]=p&p_sort[created_at]=desc

Search (search)

GET /api/users?expand[posts]=p&p_search[title]=*hello*

Filter (filter, in, between)

GET /api/users?expand[posts]=p&p_filter[status]=published
GET /api/users?expand[posts]=p&p_in[id]=1,2,3
GET /api/users?expand[posts]=p&p_between[created_at]=2000-01-01,2010-01-01

Supported Operators

Expansion supports the same operators as other modifiers:



Best Practices

Expansion makes your APIs smarter and more consumer-friendly โ€” clients can tune the exact shape of the response they want.