PHP code example of codemagpie / simple-query-builder

1. Go to this page and download the library: Download codemagpie/simple-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/ */

    

codemagpie / simple-query-builder example snippets


use CodeMagpie\SimpleQueryBuilder\AbstractSimpleQuery;

class UserQuery extends AbstractSimpleQuery
{
    /**
     * @var string[] allow the query field, ['*'] is allow all
     */
    protected array $fields = ['id', 'name', 'age'];
}


 $query = UserQuery::build()
            ->whereIn('id', [1, 2])
            ->whereEqual('name', 'test')
            ->addNestedOrWhere(function ($query) {
                $query->whereGreat('age', 10)->orWhereLess('age', 8);
            })
            ->orderByDesc('id')
            ->forPage(1, 10);
// This is similar to "where id in (1, 2) and name = 'test' or (age > 10 or age < 8) order by id desc limit 10"

$query = UserQuery::build()->whereEqual('name', 'test')->bindElasticaQueryBuilder(new \Elastica\Query());

$builder =  Model::query(); // hyperf Model, such as UserModel
$query = UserQuery::build()->whereEqual('name', 'test')->bindHyperfQueryBuilder($builder)