Appending attributes | laravel-query-builder | Spatie

 SPATIE

  Laravel Query Builder
========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-query-builder](https://spatie.be/docs/laravel-query-builder/v4)  Features  Appending attributes

 Version   v7   v6   v5   v4   v3   v2

 Other versions for crawler [v7](https://spatie.be/docs/laravel-query-builder/v7) [v6](https://spatie.be/docs/laravel-query-builder/v6) [v5](https://spatie.be/docs/laravel-query-builder/v5) [v4](https://spatie.be/docs/laravel-query-builder/v4) [v3](https://spatie.be/docs/laravel-query-builder/v3) [v2](https://spatie.be/docs/laravel-query-builder/v2)

- [ Introduction ](https://spatie.be/docs/laravel-query-builder/v4/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-query-builder/v4/requirements)
- [ About us ](https://spatie.be/docs/laravel-query-builder/v4/about-us)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-query-builder/v4/installation-setup)
- [ Support us ](https://spatie.be/docs/laravel-query-builder/v4/support-us)
- [ Questions and issues ](https://spatie.be/docs/laravel-query-builder/v4/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-query-builder/v4/changelog)

Features
--------

- [ Filtering ](https://spatie.be/docs/laravel-query-builder/v4/features/filtering)
- [ Sorting ](https://spatie.be/docs/laravel-query-builder/v4/features/sorting)
- [ Including relationships ](https://spatie.be/docs/laravel-query-builder/v4/features/including-relationships)
- [ Selecting fields ](https://spatie.be/docs/laravel-query-builder/v4/features/selecting-fields)
- [ Appending attributes ](https://spatie.be/docs/laravel-query-builder/v4/features/appending-attributes)

Advanced usage
--------------

- [ Extending query builder ](https://spatie.be/docs/laravel-query-builder/v4/advanced-usage/extending-query-builder)
- [ Pagination ](https://spatie.be/docs/laravel-query-builder/v4/advanced-usage/pagination)
- [ Front-end implementation ](https://spatie.be/docs/laravel-query-builder/v4/advanced-usage/front-end-implementation)
- [ Multi value delimiter ](https://spatie.be/docs/laravel-query-builder/v4/advanced-usage/multi-value-delimiter)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-query-builder                                                                                                                                                                                                                                    `

Appending attributes
====================

###  On this page

1. [ Basic usage ](#content-basic-usage)

When serializing a model to JSON, Laravel can add custom attributes using the [`append` method or `appends` property on the model](https://laravel.com/docs/master/eloquent-serialization#appending-values-to-json). The query builder also supports this using the `allowedAppends` method and `append` query parameter.

Basic usage
-----------------------------------------------------------------------------------------

Consider the following custom attribute accessor. It won't be included in the model's JSON by default:

```
class User extends Model
{
    public function getFullnameAttribute()
    {
        return "{$this->firstname} {$this->lastname}";
    }
}
```

Using `allowedAppends` we can optionally include the given above append.

```
// GET /users?append=fullname

$users = QueryBuilder::for(User::class)
    ->allowedAppends(['fullname'])
    ->get();
// Will call `$user->append('fullname')` on the query's results
```

Of course you can pass a list of attributes in the request to be appended:

```
// GET /users?append=fullname,ranking
```
