A simple and general query builder,You can use this builder is transformed into other query-builder, such as sql, mongodb, elasticsearch...
composer require codemagpie/simple-query-builder
Only need to extend the class \CodeMagpie\SimpleQueryBuilder\AbstractSimpleQuery :
use CodeMagpie\SimpleQueryBuilder\AbstractSimpleQuery;
class UserQuery extends AbstractSimpleQuery
{
/**
* @var string[] allow the query field, ['*'] is allow all
*/
protected array $fields = ['id', 'name', 'age'];
}
To use:
$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"
- support convert ruflin/elastica query, use:
$query = UserQuery::build()->whereEqual('name', 'test')->bindElasticaQueryBuilder(new \Elastica\Query());
- support convert hyperf/database query, use:
$builder = Model::query(); // hyperf Model, such as UserModel
$query = UserQuery::build()->whereEqual('name', 'test')->bindHyperfQueryBuilder($builder)