Spatie’s Laravel Query Builder is a great package that allows you to filter, sort and include eloquent relations based on a request. And because query parameter names follow the JSON API specification as closely as possible, this package is really a good tool when building APIs.
Scramble has Laravel Query Builder support as a part of Scramble PRO package. You can get Scramble PRO here: https://scramble.dedoc.co/pro. After purchasing, you will receive the license key to your email.
Installation instructions for `dedoc/scramble-pro`
Before installing Scramble PRO, make sure to update dedoc/scramble to at least 0.11.25.
Add the private repository to the list of your repositories in composer.json
Now, add the package to the list of your dependencies:
And run composer update.
When running composer update, you will be prompted to provide your credentials for the repository website. These credentials will authenticate your Composer session as having permission to download the Scramble PRO source code. To avoid manually typing these credentials, you may create a Composer auth.json file and use your license key in place of the password:
You may quickly create an auth.json file via your terminal using the following command.
You should not commit your application’s auth.json file into source control!
Usage
If you already configured routes for Scramble, after you install Scramble PRO – you should be all set up. Scramble will generate the query parameters for Laravel Query Builder.
Documenting filters
To define filters, you can use allowedFilters method on a query builder instance.
Also, you can add more description to a filter by adding comments and PHPDoc annotations. @example annotation can be used to specify examples, @var to specify a type, and @format to specify a format.
Available sorts are `name`, `created_at`. You can sort by multiple options by separating them with a comma. To sort in descending order, use `-` sign in front of the sort, for example: `-name`.
You can use defaultSorts (or defaultSort) method, to specify the default sort. Scramble will document it as well.
Available sorts are `name`, `created_at`. You can sort by multiple options by separating them with a comma. To sort in descending order, use `-` sign in front of the sort, for example: `-name`.
Default
-name
Documenting includes
You can allow including relationships using allowedIncludes method on a query builder.
Scramble support both passing them as a string (including a relationship, count, existence), or passing allowed includes as instances of Spatie\QueryBuilder\AllowedInclude class.
Available includes are `jobs`, `jobsCount`, `jobsExists`, `employees_count`. You can include multiple options by separating them with a comma.
Documenting fields
You can enable a selection of specific fields using allowedFields method.
You can allow selecting the fields of the model instance being queried, as well as selecting fields of the included relations. When describing allowed fields of the included relations, Scramble will separate it into a different query parameter.
Available fields are `id`, `name`, `created_at`. You can include multiple options by separating them with a comma.
fields[jobs]
string
Available fields are `id`, `title`. You can include multiple options by separating them with a comma.
Custom Query Builder class
When you want to encapsulate the logic in a query class, you can do this by extending Spatie\QueryBuilder\QueryBuilder and configuring your query builder in a constructor.
This approach can be seen in Spatie’s Laravel Query Builder documentation, in a video about building Mailcoach.
Here is the example from this video and the resulting documentation. Here Laravel Query Builder is used for the API request params, and Laravel Data for the response.
Fuzzy search subscribers by `email`, `first_name`, `last_name`, and the assigned tags `name`.
sort
string
Available sorts are `created_at`, `unsubscribed_at`, `email`, `first_name`, `last_name`. You can sort by multiple options by separating them with a comma. To sort in descending order, use `-` sign in front of the sort, for example: `-created_at`.