PHP code example of hongxunpan / eloquent-query-dsl

1. Go to this page and download the library: Download hongxunpan/eloquent-query-dsl 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/ */

    

hongxunpan / eloquent-query-dsl example snippets




use HongXunPan\EloquentQueryDsl\Definition\DslQueryDefinition;
use HongXunPan\EloquentQueryDsl\QueryDsl;

$definition = DslQueryDefinition::make('article')
    ->allowSearch(['title', 'summary'])
    ->allowFilter(['status'])
    ->allowSort(['published_at', 'id'])
    ->defaultSort('id', 'desc')
    ->strict();

$result = QueryDsl::for(Article::query(), $definition)
    ->from([
        'query' => [
            'search' => ['title' => 'alumni'],
            'filter' => ['status' => 'published'],
            'sort' => [
                ['field' => 'published_at', 'order' => 'desc'],
            ],
        ],
        'page' => ['page' => 1, 'limit' => 20],
    ])
    ->apply();

$builder = $result->builder();
$pagination = $result->pagination();
$filterValues = $result->filterValues();

$params = [
    'query' => [
        'search' => ['title' => 'alumni'],
        'filter' => ['status' => 'published'],
        'between' => ['created_at' => ['2026-01-01', '2026-12-31']],
        'sort' => [
            ['field' => 'published_at', 'order' => 'desc'],
        ],
    ],
    'page' => ['page' => 1, 'limit' => 20],
];