Overview
TheQueryBuilder class provides a fluent, convenient interface for building and executing database queries. It supports SELECT, INSERT, UPDATE, and DELETE operations with method chaining for a clean, readable syntax.
Class Reference
Lyger\Database\QueryBuilder
Fluent SQL query builder inspired by Laravel’s query builder. Location:Lyger/Database/QueryBuilder.php
Creating Query Builders
Constructor
string
required
The name of the database table
table()
string
required
The name of the database table
QueryBuilder instance
Example:
Select Methods
select()
array
required
Array of column names to select
$this for method chaining
Example:
get()
first()
null if no results
Example:
value()
string
required
The column name to retrieve
null
Example:
Where Clauses
where()
= operator.
string
required
The column name
string
Comparison operator (
=, !=, >, <, >=, <=, LIKE, etc.)mixed
The value to compare against
$this for method chaining
Example:
orWhere()
string
required
The column name
string
Comparison operator
mixed
The value to compare against
$this for method chaining
Example:
whereIn()
string
required
The column name
array
required
Array of values
$this for method chaining
Example:
whereNull()
string
required
The column name
$this for method chaining
Example:
whereNotNull()
string
required
The column name
$this for method chaining
Example:
Ordering and Limiting
orderBy()
string
required
The column to order by
string
default:"'ASC'"
Sort direction: ‘ASC’ or ‘DESC’
$this for method chaining
Example:
latest()
string
default:"'created_at'"
The column to order by
$this for method chaining
Example:
oldest()
string
default:"'created_at'"
The column to order by
$this for method chaining
Example:
limit()
int
required
Maximum number of results
$this for method chaining
Example:
offset()
int
required
Number of results to skip
$this for method chaining
Example:
Pagination
paginate()
int
default:"15"
Number of items per page
int
default:"1"
Current page number
Joins
join()
string
required
The table to join
string
required
First column in the join condition
string
required
Comparison operator (usually ’=’)
string
required
Second column in the join condition
$this for method chaining
Example:
leftJoin()
string
required
The table to join
string
required
First column in the join condition
string
required
Comparison operator (usually ’=’)
string
required
Second column in the join condition
$this for method chaining
Example:
Aggregates
count()
exists()
true if records exist, false otherwise
Example:
Insert, Update, Delete
insert()
array
required
Associative array of column => value pairs
true on success, false on failure
Example:
update()
array
required
Associative array of column => value pairs to update
delete()
Always use WHERE clauses with update() and delete() to avoid accidentally modifying/removing all records!
Usage Examples
Complex Query
Join with Conditions
Conditional Updates
Bulk Operations
Best Practices
- Always use parameter binding (automatically handled by QueryBuilder) to prevent SQL injection
- Use
limit()with potentially large result sets to control memory usage - Add indexes to columns frequently used in WHERE and JOIN clauses for better performance
- Use
exists()instead ofcount() > 0when you only need to check if records exist - Chain method calls for readable, maintainable query code
Connection Management
The QueryBuilder automatically manages database connections using PDO. It connects to a SQLite database by default, located atdatabase/database.sqlite.