QueryBuilder
in package
Fluent query builder for models.
Provides a Rails-like interface for querying models with support for eager loading relations.
Table of Contents
Properties
- $_bindings : array<string|int, mixed>
- $_distinct : bool
- $_groupBy : array<string|int, mixed>
- $_joins : array<string|int, mixed>
- $_limit : int|null
- $_modelClass : string
- $_offset : int|null
- $_orderBy : array<string|int, mixed>
- $_pdo : PDO
- $_select : array<string|int, mixed>
- $_table : string
- $_tableAlias : string|null
- $_wheres : array<string|int, mixed>
- $_with : array<string|int, mixed>
Methods
- __construct() : mixed
- Constructor
- addSelect() : $this
- Add columns to the existing select list.
- all() : array<string|int, mixed>
- Get all results (alias for get).
- avg() : mixed
- Get the average of a column.
- count() : int
- Count results.
- crossJoin() : $this
- Add a CROSS JOIN clause.
- decrement() : int
- Atomically decrement a column's value.
- delete() : int
- Delete records matching the query.
- distinct() : $this
- Add DISTINCT to the query.
- find() : Model|null
- Find a model by primary key.
- first() : Model|null
- Get the first result.
- from() : $this
- Set the table to select from with optional alias.
- get() : array<string|int, mixed>
- Get all results.
- getRaw() : array<string|int, mixed>
- Get all results as raw arrays without hydrating into models.
- groupBy() : $this
- Add a GROUP BY clause.
- increment() : int
- Atomically increment a column's value.
- join() : $this
- Add an INNER JOIN clause.
- leftJoin() : $this
- Add a LEFT JOIN clause.
- limit() : $this
- Set result limit.
- max() : mixed
- Get the maximum value of a column.
- min() : mixed
- Get the minimum value of a column.
- offset() : $this
- Set result offset.
- orderBy() : $this
- Add an order by clause.
- orWhere() : $this
- Add an OR where clause.
- rightJoin() : $this
- Add a RIGHT JOIN clause.
- select() : $this
- Set the columns to select.
- selectRaw() : $this
- Add a raw select expression.
- sum() : mixed
- Get the sum of a column.
- update() : int
- Atomically update column values.
- where() : $this
- Add a where clause.
- whereIn() : $this
- Add a WHERE IN clause.
- with() : $this
- Specify relations to eager load.
- addJoin() : $this
- Add a join to the query.
- aggregate() : mixed
- Execute an aggregate function.
- buildSql() : string
- Build the SQL query.
- buildWhereClause() : string
- Build the WHERE clause.
Properties
$_bindings
private
array<string|int, mixed>
$_bindings
= []
$_distinct
private
bool
$_distinct
= false
$_groupBy
private
array<string|int, mixed>
$_groupBy
= []
$_joins
private
array<string|int, mixed>
$_joins
= []
$_limit
private
int|null
$_limit
= null
$_modelClass
private
string
$_modelClass
$_offset
private
int|null
$_offset
= null
$_orderBy
private
array<string|int, mixed>
$_orderBy
= []
$_pdo
private
PDO
$_pdo
$_select
private
array<string|int, mixed>
$_select
= ['*']
$_table
private
string
$_table
$_tableAlias
private
string|null
$_tableAlias
= null
$_wheres
private
array<string|int, mixed>
$_wheres
= []
$_with
private
array<string|int, mixed>
$_with
= []
Methods
__construct()
Constructor
public
__construct(PDO $pdo, string $modelClass) : mixed
Parameters
- $pdo : PDO
-
Database connection
- $modelClass : string
-
Model class name
addSelect()
Add columns to the existing select list.
public
addSelect(string|array<string|int, mixed> $columns) : $this
Parameters
- $columns : string|array<string|int, mixed>
-
Column name(s) to add
Return values
$thisall()
Get all results (alias for get).
public
all() : array<string|int, mixed>
Return values
array<string|int, mixed>avg()
Get the average of a column.
public
avg(string $column) : mixed
Parameters
- $column : string
count()
Count results.
public
count() : int
Return values
intcrossJoin()
Add a CROSS JOIN clause.
public
crossJoin(string $table) : $this
Parameters
- $table : string
-
Table name (can include alias, e.g., "users u")
Return values
$thisdecrement()
Atomically decrement a column's value.
public
decrement(string $column[, int $amount = 1 ]) : int
This method performs an atomic UPDATE query that decrements the column value by the specified amount. This avoids race conditions that occur with the fetch-decrement-save pattern under concurrent requests.
Parameters
- $column : string
-
The column to decrement
- $amount : int = 1
-
The amount to decrement by (default: 1)
Return values
int —Number of rows updated
delete()
Delete records matching the query.
public
delete() : int
Return values
int —Number of rows deleted
distinct()
Add DISTINCT to the query.
public
distinct() : $this
Return values
$thisfind()
Find a model by primary key.
public
find(int $id) : Model|null
Parameters
- $id : int
Return values
Model|nullfirst()
Get the first result.
public
first() : Model|null
Return values
Model|nullfrom()
Set the table to select from with optional alias.
public
from(string $table[, string|null $alias = null ]) : $this
Parameters
- $table : string
-
Table name
- $alias : string|null = null
-
Optional table alias
Return values
$thisget()
Get all results.
public
get() : array<string|int, mixed>
Return values
array<string|int, mixed>getRaw()
Get all results as raw arrays without hydrating into models.
public
getRaw() : array<string|int, mixed>
This is useful for queries with aggregate functions, computed columns, or when you need the raw database results without model overhead.
Return values
array<string|int, mixed> —Array of associative arrays
groupBy()
Add a GROUP BY clause.
public
groupBy(string|array<string|int, mixed> $columns) : $this
Parameters
- $columns : string|array<string|int, mixed>
-
Column name(s) to group by
Return values
$thisincrement()
Atomically increment a column's value.
public
increment(string $column[, int $amount = 1 ]) : int
This method performs an atomic UPDATE query that increments the column value by the specified amount. This avoids race conditions that occur with the fetch-increment-save pattern under concurrent requests.
Parameters
- $column : string
-
The column to increment
- $amount : int = 1
-
The amount to increment by (default: 1)
Return values
int —Number of rows updated
join()
Add an INNER JOIN clause.
public
join(string $table, string $first, string $operator, string $second) : $this
Parameters
- $table : string
-
Table name (can include alias, e.g., "users u")
- $first : string
-
First column in join condition
- $operator : string
-
Operator (=, !=, <, >, etc.)
- $second : string
-
Second column in join condition
Return values
$thisleftJoin()
Add a LEFT JOIN clause.
public
leftJoin(string $table, string $first, string $operator, string $second) : $this
Parameters
- $table : string
-
Table name (can include alias, e.g., "users u")
- $first : string
-
First column in join condition
- $operator : string
-
Operator (=, !=, <, >, etc.)
- $second : string
-
Second column in join condition
Return values
$thislimit()
Set result limit.
public
limit(int $limit) : $this
Parameters
- $limit : int
Return values
$thismax()
Get the maximum value of a column.
public
max(string $column) : mixed
Parameters
- $column : string
min()
Get the minimum value of a column.
public
min(string $column) : mixed
Parameters
- $column : string
offset()
Set result offset.
public
offset(int $offset) : $this
Parameters
- $offset : int
Return values
$thisorderBy()
Add an order by clause.
public
orderBy(string $column[, string $direction = 'ASC' ]) : $this
Parameters
- $column : string
- $direction : string = 'ASC'
Return values
$thisorWhere()
Add an OR where clause.
public
orWhere(string $column, mixed $operator[, mixed|null $value = null ]) : $this
Parameters
- $column : string
- $operator : mixed
- $value : mixed|null = null
Return values
$thisrightJoin()
Add a RIGHT JOIN clause.
public
rightJoin(string $table, string $first, string $operator, string $second) : $this
Parameters
- $table : string
-
Table name (can include alias, e.g., "users u")
- $first : string
-
First column in join condition
- $operator : string
-
Operator (=, !=, <, >, etc.)
- $second : string
-
Second column in join condition
Return values
$thisselect()
Set the columns to select.
public
select(string|array<string|int, mixed> $columns) : $this
Parameters
- $columns : string|array<string|int, mixed>
-
Column name(s) to select
Return values
$thisselectRaw()
Add a raw select expression.
public
selectRaw(string $expression) : $this
Parameters
- $expression : string
-
Raw SQL expression
Return values
$thissum()
Get the sum of a column.
public
sum(string $column) : mixed
Parameters
- $column : string
update()
Atomically update column values.
public
update(array<string|int, mixed> $attributes) : int
This method performs an atomic UPDATE query that sets multiple columns to new values. This avoids race conditions that occur with the fetch-modify-save pattern under concurrent requests.
Parameters
- $attributes : array<string|int, mixed>
-
Associative array of column => value pairs
Return values
int —Number of rows updated
where()
Add a where clause.
public
where(string $column, mixed $operator[, mixed|null $value = null ]) : $this
Parameters
- $column : string
- $operator : mixed
- $value : mixed|null = null
Return values
$thiswhereIn()
Add a WHERE IN clause.
public
whereIn(string $column, array<string|int, mixed> $values) : $this
Parameters
- $column : string
- $values : array<string|int, mixed>
Return values
$thiswith()
Specify relations to eager load.
public
with(array<string|int, mixed>|string $relations) : $this
Parameters
- $relations : array<string|int, mixed>|string
Return values
$thisaddJoin()
Add a join to the query.
protected
addJoin(string $type, string $table, string $first, string $operator, string $second) : $this
Parameters
- $type : string
-
Join type (INNER, LEFT, RIGHT)
- $table : string
-
Table name
- $first : string
-
First column in join condition
- $operator : string
-
Operator
- $second : string
-
Second column in join condition
Return values
$thisaggregate()
Execute an aggregate function.
protected
aggregate(string $function, string $column) : mixed
Parameters
- $function : string
- $column : string
buildSql()
Build the SQL query.
protected
buildSql() : string
Return values
stringbuildWhereClause()
Build the WHERE clause.
protected
buildWhereClause() : string