PHP code example of omaressaouaf / query-builder-criteria

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

    

omaressaouaf / query-builder-criteria example snippets


class PostCriteria extends Criteria
{
    protected array $filters = ['title', 'slug', 'user'];

    protected array $sorts = ['published_at', 'created_at'];

    protected array $

$posts = Post::query()->queryByCriteria(PostCriteria::class)->get();



return [
    /**
     * The name of the search_query_parameter inside the query string
     * For example: GET /users?search_query=john
     */
    'search_query_parameter' => 'search_query',

    /**
     * Split the search query into an array of terms to compare to
     * This is applied for all criteria unless overridden inside the criteria class
     */
    'split_search_into_terms' => false,

    /**
     * Define a default sorts for all criteria
     * This is applied for all criteria unless overridden inside the criteria class
     *
     * Possible value types: array, string, null
     */
    'default_sorts' => null,
];


namespace App\Criteria;

use Omaressaouaf\QueryBuilderCriteria\Criteria as BaseCriteria;

class PostCriteria extends BaseCriteria
{
    protected array $filters = ['title'];

    protected array $exactFilters = ['id', 'slug'];

    protected array $sorts = ['published_at'];

    protected array $

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Omaressaouaf\QueryBuilderCriteria\QueryableByCriteria;
use Omaressaouaf\QueryBuilderCriteria\Tests\Criteria\PostCriteria;

class Post extends Model
{
    use QueryableByCriteria;

    protected function defaultQueryBuilderCriteria(): array|string
    {
        return PostCriteria::class;
    }
}

Post::query()->queryByCriteria()->get();

GET /posts?filter[title]=Hello&

protected array $filters = ['title'];

protected array $exactFilters = ['id', 'slug'];

protected array $belongsToFilters = ['user'];

protected array $scopeFilters = ['published_before'];

protected string|bool|array|null $trashedFilter = true;

protected array|string $defaultSorts = '-created_at';

protected array $sorts = ['published_at', 'created_at'];

protected array $ $countIncludes = ['comments'];

protected array $existsIncludes = ['comments'];

protected array $defaultFields = ['id', 'slug', 'title'];

protected array $fields = ['id', 'slug', 'title', 'body', 'published_at', 'user_id', 'created_at', 'updated_at'];

protected array $searches = ['slug', 'title', 'user.name'];

protected array $fullTextSearches = ['body', 'user.bio'];

protected ?bool $splitSearchIntoTerms = false;

protected array $filters = [
    'name' => 'user_passport_full_name'
];

use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\AllowedSort;
use Spatie\QueryBuilder\AllowedInclude;

protected function advancedFilters(): array
{
    return [
        AllowedFilter::callback('cb_filter_1', fn () => ''),
    ];
}

protected function advancedDefaultSorts(): array
{
    return [
        AllowedSort::callback('cb_sort_1', fn () => ''),
    ];
}

protected function advancedSorts(): array
{
    return [
        AllowedSort::callback('cb_sort_1', fn () => ''),
    ];
}

protected function advancedIncludes(): array
{
    return [
        AllowedInclude::callback('cb_

namespace App\Models\Models;

use Illuminate\Database\Eloquent\Model;
use Omaressaouaf\QueryBuilderCriteria\QueryableByCriteria;
use App\Criteria\PostCriteria;

class Post extends Model
{
    use QueryableByCriteria;

    protected function defaultQueryBuilderCriteria(): array|string
    {
        return PostCriteria::class;
    }
}

return Post::query()->queryByCriteria()->get();

return Post::query()->queryByCriteria(CustomCriteria::class, AnotherCriteria::class)->get();
bash
php artisan vendor:publish --provider="Omaressaouaf\QueryBuilderCriteria\QueryBuilderCriteriaServiceProvider"