Skip to main content

Overview

The QueryBuilder 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

Create a new query builder instance for a specific table.
string
required
The name of the database table
Example:

table()

Static method to create a query builder instance.
string
required
The name of the database table
Returns: QueryBuilder instance Example:

Select Methods

select()

Set the columns to be selected.
array
required
Array of column names to select
Returns: $this for method chaining Example:

get()

Execute the query and get all results. Returns: Array of associative arrays Example:

first()

Execute the query and get the first result. Returns: Associative array or null if no results Example:

value()

Execute the query and get a single column value from the first result.
string
required
The column name to retrieve
Returns: Column value or null Example:

Where Clauses

where()

Add a WHERE clause to the query. If only two parameters provided, assumes = operator.
string
required
The column name
string
Comparison operator (=, !=, >, <, >=, <=, LIKE, etc.)
mixed
The value to compare against
Returns: $this for method chaining Example:

orWhere()

Add an OR WHERE clause to the query.
string
required
The column name
string
Comparison operator
mixed
The value to compare against
Returns: $this for method chaining Example:

whereIn()

Add a WHERE IN clause to the query.
string
required
The column name
array
required
Array of values
Returns: $this for method chaining Example:

whereNull()

Add a WHERE IS NULL clause to the query.
string
required
The column name
Returns: $this for method chaining Example:

whereNotNull()

Add a WHERE IS NOT NULL clause to the query.
string
required
The column name
Returns: $this for method chaining Example:

Ordering and Limiting

orderBy()

Add an ORDER BY clause to the query.
string
required
The column to order by
string
default:"'ASC'"
Sort direction: ‘ASC’ or ‘DESC’
Returns: $this for method chaining Example:

latest()

Order results by a column in descending order.
string
default:"'created_at'"
The column to order by
Returns: $this for method chaining Example:

oldest()

Order results by a column in ascending order.
string
default:"'created_at'"
The column to order by
Returns: $this for method chaining Example:

limit()

Limit the number of results returned.
int
required
Maximum number of results
Returns: $this for method chaining Example:

offset()

Skip a specified number of results.
int
required
Number of results to skip
Returns: $this for method chaining Example:

Pagination

paginate()

Paginate query results.
int
default:"15"
Number of items per page
int
default:"1"
Current page number
Returns: Pagination array with data and metadata Example:

Joins

join()

Add an INNER JOIN clause to the query.
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
Returns: $this for method chaining Example:

leftJoin()

Add a LEFT JOIN clause to the query.
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
Returns: $this for method chaining Example:

Aggregates

count()

Get the count of results matching the query. Returns: Integer count Example:

exists()

Check if any records exist matching the query. Returns: true if records exist, false otherwise Example:

Insert, Update, Delete

insert()

Insert a new record into the database.
array
required
Associative array of column => value pairs
Returns: true on success, false on failure Example:

update()

Update records matching the query.
array
required
Associative array of column => value pairs to update
Returns: Number of affected rows Example:

delete()

Delete records matching the query. Returns: Number of deleted rows Example:
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 of count() > 0 when 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 at database/database.sqlite.
  • Model - Eloquent-style ORM models
  • Schema - Database schema builder
  • Migration - Database migration system