PHP code example of roundly-consulting / query-builder-for-laravel

1. Go to this page and download the library: Download roundly-consulting/query-builder-for-laravel 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/ */

    

roundly-consulting / query-builder-for-laravel example snippets


use RoundlyConsulting\QueryBuilder\AllowedFilter;
use RoundlyConsulting\QueryBuilder\AllowedSort;
use RoundlyConsulting\QueryBuilder\QueryBuilder;

// GET /posts?filter[status]=published,draft&filter[title]=laravel&sort=-popularity&per_page=50
public function index(ListPostsRequest $request)
{
    return QueryBuilder::for(Post::class)
        ->allowedFilters(
            AllowedFilter::exact('status'),                   // a comma list becomes whereIn
            AllowedFilter::partial('title'),                  // escaped LIKE, ILIKE on Postgres
            AllowedFilter::scope('published', booleans: true),
            AllowedFilter::trashed(),
        )
        ->allowedSorts('title', AllowedSort::field('popularity', 'views'))
        ->defaultSort('-created_at')
        ->paginate($request->perPage());
}

use Illuminate\Foundation\Http\FormRequest;
use RoundlyConsulting\QueryBuilder\Concerns\HasPageSize;

final class ListPostsRequest extends FormRequest
{
    use HasPageSize;

    public function rules(): array
    {
        return $this->pageSizeRules();
    }
}