Skip to content

Query

Raxos\Database\Query\Query is the fluent SQL query builder returned by Connection::query(), Db::query() and Model::query(). It collects the statement piece by piece and executes it through a prepared Statement. Each driver returns a dialect specific subclass, but the public surface below is identical.

php
abstract class Query implements QueryInterface

Selecting and sources

MethodDescription
select(Select|Stringable|array|string|int $fields = []): staticAdds or extends the select clause. Empty means *.
selectDistinct(Select|Stringable|array|string|int $fields = []): staticA select distinct clause.
selectSuffix(string $suffix, ...$fields): staticA select with a raw suffix after the keyword.
from(QueryInterface|array|string $tables, ?string $alias = null): staticSets the from clause, accepting table names or a sub query.

Conditions

MethodDescription
where(...$lhs, ...$cmp, ...$rhs): staticAdds a where condition, chaining with and. Two arguments imply =.
orWhere(...): staticAdds a where condition chained with or.
whereNull() / whereNotNull()Adds a null check.
whereIn() / whereNotIn()Adds an in / not in check.
whereExists(QueryInterface) / whereNotExists()Adds an exists / not exists sub query.
whereHas(string $relation, ?callable $fn = null): staticAdds a where exists based on a model relation.
whereRelation(string $relation, ...): staticA whereHas() that applies a comparison inside the relation.
having(...), havingIn(), havingNull(), ...The same family for the having clause.

Joins, grouping and ordering

MethodDescription
join(), innerJoin(), leftJoin(), leftOuterJoin(), rightJoin(), fullJoin()The join family; each takes a table and an optional callback for on() conditions.
on(...): staticAdds a join condition, chaining with and.
groupBy(QueryLiteralInterface|array|string $fields, bool $withRollup = false): staticAdds a group by clause.
orderBy(QueryLiteralInterface|array|string $fields): staticAdds an order by clause.
orderByAsc() / orderByDesc()Explicit ascending / descending order.
limit(int $limit, int $offset = 0): staticSets the row limit and optional offset.
offset(int $offset): staticSets the offset on its own.

Writes

MethodDescription
insertInto(string $table, array $fields): staticStarts an insert with the given columns.
insertIntoValues(string $table, array $pairs): staticBuilds an insert from a column to value map or a list of rows.
insertIgnoreIntoValues() / replaceIntoValues()Insert ignore and replace variants.
values(array $values): staticAdds a row of values.
onDuplicateKeyUpdate(array|string $fields): staticAdds an on duplicate key update clause.
update(string $table, ?array $pairs = null): staticStarts an update, optionally setting column to value pairs.
set(field, value): staticAdds a single assignment to an update.
deleteFrom(string $table): staticBuilds a delete for the table.

Executing

MethodDescription
array(int $fetchMode = PDO::FETCH_ASSOC): arrayReturns all rows as an array.
arrayList(int $fetchMode = PDO::FETCH_ASSOC): ArrayListInterface|ModelArrayListReturns the rows as an ArrayList, or a ModelArrayList for model queries.
single(int $fetchMode = PDO::FETCH_ASSOC): Model|stdClass|array|nullReturns the first row or null.
singleOrFail(...): Model|stdClass|arraySame as single(), but throws MissingResultException when nothing is found.
cursor(int $fetchMode = PDO::FETCH_ASSOC): GeneratorYields rows one at a time.
run(array $options = []): intExecutes a write and returns the affected row count.
paginate(int $offset, int $limit, ?callable $itemBuilder = null, ?callable $totalBuilder = null): PaginatedExecutes as a page of results, including a total count.
toSql(): stringCompiles the query to a SQL string.

Model helpers

MethodDescription
withModel(string $class): staticBinds the query to a model, so rows hydrate into instances.
withoutModel(): staticRemoves the model binding.
eagerLoad(string|array $relations): staticEager loads the given relations.
eagerLoadDisable(string|array $relations): staticDisables eager loading of the given relations.
withDeleted(): staticIncludes soft deleted rows for a model with #[SoftDelete].

Example

php
<?php
declare(strict_types=1);

use Raxos\Database\Db;
use function Raxos\Database\Query\literal;

$rows = Db::query()
    ->select(['id', 'name'])
    ->from('users')
    ->where('is_active', 1)
    ->where('created_on', '>', literal('now() - interval 30 day'))
    ->orderByDesc('created_on')
    ->limit(50)
    ->array();

For expressions inside where, having and select, see Expr. For how rows are fetched and hydrated, see Statement.