PHP code example of lokiwich / scramble-spatie-query-builder

1. Go to this page and download the library: Download lokiwich/scramble-spatie-query-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lokiwich / scramble-spatie-query-builder example snippets


use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

public function index()
{
    $users = QueryBuilder::for(User::class)
        ->allowedFields(['id', 'name', 'email'])
        ->allowedFilters([
            'name',
            AllowedFilter::exact('email'),
            AllowedFilter::groupOr('q', [
                AllowedFilter::partial('name'),
                AllowedFilter::partial('email'),
            ]),
        ])
        ->allowedIncludes('posts', 'roles')
        ->allowedSorts('name', 'created_at')
        ->paginate();

    return UserResource::collection($users);
}

use Lokiwich\ScrambleSpatieQueryBuilder\Extensions\DocumentsAllowedIncludes;
use Dedoc\Scramble\Support\Generator\Operation;
use Dedoc\Scramble\Support\Generator\Parameter;

DocumentsAllowedIncludes::hook(function (Operation $operation, Parameter $parameter) {
    $parameter->description('Eager-load related resources');
});