> For the complete documentation index, see [llms.txt](https://docs.opensoutheners.com/home/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.opensoutheners.com/home/laravel-apiable/request-features/appends.md).

# Appends

Append computed model accessors to your JSON:API resource attributes.

Appends allow API consumers to request computed [model accessors](https://laravel.com/docs/eloquent-mutators#defining-an-accessor) alongside the standard resource attributes. Unlike sparse fieldsets, appends do not affect the database `SELECT` — they are applied to the loaded model instances after the query completes.

## URL format

```
GET /posts?appends[post]=is_featured
GET /posts?appends[post]=is_featured,reading_time&appends[user]=avatar_url
```

The key inside `appends[]` is the JSON:API resource type. The value is a comma-separated list of accessor names.

## `AllowedAppends::make()`

`AllowedAppends::make()` takes a resource type and an array (or string) of accessor names:

```php
use OpenSoutheners\LaravelApiable\Http\AllowedAppends;

AllowedAppends::make('post', ['is_featured', 'reading_time'])
AllowedAppends::make('user', ['avatar_url'])
```

Instead of a string resource type, you can pass the model class directly — the package resolves the type from your `resource_type_map` config:

```php
use App\Models\User;

AllowedAppends::make(User::class, ['avatar_url'])
```

## Allowing appends

{% tabs %}
{% tab title="Using methods" %}
Pass `AllowedAppends` instances to `allowing()`, or call `allowAppends()` directly:

```php
use OpenSoutheners\LaravelApiable\Http\JsonApiResponse;
use OpenSoutheners\LaravelApiable\Http\AllowedAppends;
use App\Models\User;

public function index()
{
    return JsonApiResponse::from(Post::class)
        ->allowing([
            AllowedAppends::make('post', ['is_featured', 'reading_time']),
            AllowedAppends::make('user', ['avatar_url']),
        ]);
}
```

Using `allowAppends()` directly with a string type:

```php
return JsonApiResponse::from(Post::class)
    ->allowAppends('post', ['is_featured', 'reading_time'])
    ->allowAppends('user', ['avatar_url']);
```

Using a model class for the type argument:

```php
return JsonApiResponse::from(Post::class)
    ->allowAppends(User::class, ['avatar_url']);
```

Shorthand — pass an array as the first argument to target the main resource type:

```php
return JsonApiResponse::from(Post::class)
    ->allowAppends(['is_featured']);
```

{% endtab %}

{% tab title="Using attributes" %}

```php
use OpenSoutheners\LaravelApiable\Attributes\AppendsQueryParam;
use OpenSoutheners\LaravelApiable\Http\JsonApiResponse;
use App\Models\User;

#[AppendsQueryParam('post', ['is_featured', 'reading_time'])]
#[AppendsQueryParam(User::class, ['avatar_url'])]
public function index(JsonApiResponse $response)
{
    return $response->using(Post::class);
}
```

`AppendsQueryParam` accepts: `type` (string resource type or model class-string), `attributes` (array of accessor names), and `description`.
{% endtab %}
{% endtabs %}

## Fields vs. appends

|                               | `allowFields()`         | `allowAppends()`     |
| ----------------------------- | ----------------------- | -------------------- |
| Affects `SELECT` query        | Yes — limits DB columns | No                   |
| Works with DB columns         | Yes                     | No                   |
| Works with computed accessors | No                      | Yes                  |
| Applied at                    | Query build time        | After query executes |

Use `allowFields()` for real database columns when you want to reduce data transfer. Use `allowAppends()` for PHP-computed values (accessors defined with `Attribute::make()` or `get*Attribute()` methods) that have no corresponding database column.

## Forcing appends unconditionally

If you want to always append specific accessors regardless of what the consumer requests, use `forceAppend()` on `JsonApiResponse`. See the [JsonApiResponse documentation](/home/laravel-apiable/responses/json-api-response.md) for details.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.opensoutheners.com/home/laravel-apiable/request-features/appends.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
