zaruto / scramble-spatie-query-builder
This is the Scramble extension, which detects the usage of the Spatie query builder and adds applicable query parameters to the openapi definitions
Package info
github.com/zaruto/scramble-spatie-query-builder
pkg:composer/zaruto/scramble-spatie-query-builder
Requires
- php: ^8.3
- dedoc/scramble: ^0.13.16
- spatie/laravel-query-builder: ^7.0.1
Requires (Dev)
- laravel/pint: ^1.29
- nunomaduro/collision: ^8.9.1
- orchestra/testbench: ^10.11
- pestphp/pest: ^4.4.1
- pestphp/pest-plugin-laravel: ^4.1
README
Introduction
This is the Scramble extension, which detects the usage of the Spatie query builder in your api routes and automatically adds applicable query parameters to the openapi definition.
Installation
After you publish the package on Packagist:
composer require zaruto/scramble-spatie-query-builder
Until the package is published, install it directly from your GitHub repository:
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/zaruto/scramble-spatie-query-builder.git"
}
],
"require": {
"zaruto/scramble-spatie-query-builder": "dev-main"
}
}
Then run:
composer update zaruto/scramble-spatie-query-builder
Supported baseline:
- PHP
^8.3 dedoc/scramble^0.13.16spatie/laravel-query-builder^7.0.1- Laravel 12 test/dev stack
Usage
- Register the extension in your
config/scramble.phpfile.
'extensions' => [ // ... \Zaruto\ScrambleSpatieQueryBuilder\AllowedFieldsExtension::class, \Zaruto\ScrambleSpatieQueryBuilder\AllowedSortsExtension::class, \Zaruto\ScrambleSpatieQueryBuilder\AllowedFiltersExtension::class, \Zaruto\ScrambleSpatieQueryBuilder\AllowedIncludesExtension::class, // \Zaruto\ScrambleSpatieQueryBuilder\AllowedFilterModesExtension::class ],
- Use Spatie Query Builder in your controller or route action as usual.
<?php namespace App\Http\Controllers; use App\Models\User; use Illuminate\Http\Resources\Json\AnonymousResourceCollection; use Spatie\QueryBuilder\AllowedFilter; use Spatie\QueryBuilder\QueryBuilder; class UserController extends Controller { public function index(): AnonymousResourceCollection { $users = QueryBuilder::for(User::query()) ->allowedFields(['id', 'name', 'email']) ->allowedFilters([ 'name', AllowedFilter::exact('email'), ]) ->allowedIncludes(['posts', 'roles']) ->allowedSorts(['name', 'created_at']) ->paginate(); return UserResource::collection($users); } }
- Open your Scramble docs. For routes that use Spatie Query Builder, the extension will add query parameters such as:
fieldsfilterincludesortfilter_modewhenAllowedFilterModesExtensionis enabled
- Example query string:
/api/users?fields[users]=id,name,email&filter[name]=john&include=posts&sort=-created_at
Customization
By default this extension automatically updates openapi definition for you, but if you want to customize its default behaviour, you can do it in the following way
- Open your
AppServiceProvider.phpand add the following code example in thebootmethod
public function boot(): void { // ... AllowedIncludesExtension::hook(function(Operation $operation, Parameter $parameter) { // Customize the example $parameter->example(['repositories.issues', 'repositories']); // Customize the description $parameter->description('Allows you to include additional model relations in the response'); }); }
- Customize for your needs
Publishing
To publish this as your own package:
- Create a public GitHub repository named
scramble-spatie-query-builderunder yourzarutoaccount. - Push this code to
https://github.com/zaruto/scramble-spatie-query-builder. - Sign in to Packagist with your GitHub account.
- Submit the repository URL to Packagist.
- After Packagist indexes the repository, install it with
composer require zaruto/scramble-spatie-query-builder. - Add the Packagist service hook in GitHub so new tags and releases are picked up automatically.
