Neuron-PHP

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 = []

$_groupBy

private array<string|int, mixed> $_groupBy = []

$_orderBy

private array<string|int, mixed> $_orderBy = []

$_select

private array<string|int, mixed> $_select = ['*']

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
$this

all()

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
int

crossJoin()

Add a CROSS JOIN clause.

public crossJoin(string $table) : $this
Parameters
$table : string

Table name (can include alias, e.g., "users u")

Return values
$this

decrement()

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
$this

find()

Find a model by primary key.

public find(int $id) : Model|null
Parameters
$id : int
Return values
Model|null

from()

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
$this

get()

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
$this

increment()

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
$this

leftJoin()

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
$this

limit()

Set result limit.

public limit(int $limit) : $this
Parameters
$limit : int
Return values
$this

max()

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
$this

orderBy()

Add an order by clause.

public orderBy(string $column[, string $direction = 'ASC' ]) : $this
Parameters
$column : string
$direction : string = 'ASC'
Return values
$this

orWhere()

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
$this

rightJoin()

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
$this

select()

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
$this

selectRaw()

Add a raw select expression.

public selectRaw(string $expression) : $this
Parameters
$expression : string

Raw SQL expression

Return values
$this

sum()

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
$this

whereIn()

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
$this

with()

Specify relations to eager load.

public with(array<string|int, mixed>|string $relations) : $this
Parameters
$relations : array<string|int, mixed>|string
Return values
$this

addJoin()

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
$this

aggregate()

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
string

buildWhereClause()

Build the WHERE clause.

protected buildWhereClause() : string
Return values
string

        
On this page

Search results