API Reference
Table of Contents
- Connection Methods
- Query Builder Methods
- Select Statements
- Where Clauses
- Joins
- Ordering, Grouping & Limiting
- Aggregates
- Insert Statements
- Update Statements
- Delete Statements
- Utility Methods
Connection Methods
Creating a Connection
use Bob\Database\Connection;
$connection = new Connection([
'driver' => 'mysql', // mysql, pgsql, sqlite
'host' => 'localhost',
'port' => 3306,
'database' => 'mydb',
'username' => 'user',
'password' => 'pass',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
]);table($table)
Get a query builder for a table.
$query = $connection->table('users');raw($value)
Create a raw SQL expression.
$expression = $connection->raw('COUNT(*) as total');statement($query, $bindings = [])
Execute a raw SQL statement.
$connection->statement('CREATE TABLE test (id INT)');select($query, $bindings = [])
Run a raw select query.
$results = $connection->select('SELECT * FROM users WHERE id = ?', [1]);insert($query, $bindings = [])
Run a raw insert query.
$connection->insert('INSERT INTO users (name) VALUES (?)', ['John']);update($query, $bindings = [])
Run a raw update query.
$affected = $connection->update('UPDATE users SET name = ? WHERE id = ?', ['Jane', 1]);delete($query, $bindings = [])
Run a raw delete query.
$deleted = $connection->delete('DELETE FROM users WHERE id = ?', [1]);transaction($callback, $attempts = 1)
Execute a closure within a transaction.
$connection->transaction(function () use ($connection) {
$connection->table('users')->insert(['name' => 'John']);
$connection->table('posts')->insert(['title' => 'Hello']);
});beginTransaction()
Start a new database transaction.
$connection->beginTransaction();commit()
Commit the active database transaction.
$connection->commit();rollBack()
Rollback the active database transaction.
$connection->rollBack();Query Builder Methods
Creating a Query
$query = $connection->table('users');Select Statements
select($columns = ['*'])
Set the columns to be selected.
$query->select('id', 'name', 'email');
$query->select(['id', 'name']);addSelect($column)
Add a column to the existing select.
$query->select('id')->addSelect('name');selectRaw($expression, $bindings = [])
Add a raw select expression.
$query->selectRaw('COUNT(*) as user_count');
$query->selectRaw('price * ? as total', [1.0825]);distinct()
Force the query to return distinct results.
$query->distinct()->select('status');from($table, $as = null)
Set the table for the query.
$query->from('users');
$query->from('users', 'u');Where Clauses
where($column, $operator = null, $value = null, $boolean = 'and')
Add a basic where clause.
$query->where('votes', '>', 100);
$query->where('name', 'John'); // Uses '=' operator
$query->where([
['status', '=', 'active'],
['votes', '>', 100]
]);orWhere($column, $operator = null, $value = null)
Add an OR where clause.
$query->where('votes', '>', 100)->orWhere('name', 'John');whereIn($column, $values, $boolean = 'and', $not = false)
Add a where in clause.
$query->whereIn('id', [1, 2, 3]);
$query->whereIn('id', function($query) {
$query->select('user_id')->from('posts');
});whereNotIn($column, $values, $boolean = 'and')
Add a where not in clause.
$query->whereNotIn('id', [1, 2, 3]);whereNull($column, $boolean = 'and', $not = false)
Add a where null clause.
$query->whereNull('deleted_at');whereNotNull($column, $boolean = 'and')
Add a where not null clause.
$query->whereNotNull('email_verified_at');orWhereNull($column)
Add an OR where null clause.
$query->orWhereNull('deleted_at');orWhereNotNull($column)
Add an OR where not null clause.
$query->orWhereNotNull('email_verified_at');whereBetween($column, array $values, $boolean = 'and', $not = false)
Add a where between clause.
$query->whereBetween('votes', [1, 100]);whereNotBetween($column, array $values, $boolean = 'and')
Add a where not between clause.
$query->whereNotBetween('votes', [1, 100]);whereExists(Closure $callback, $boolean = 'and', $not = false)
Add a where exists clause.
$query->whereExists(function($query) {
$query->select('*')
->from('orders')
->whereColumn('orders.user_id', 'users.id');
});whereNotExists(Closure $callback, $boolean = 'and')
Add a where not exists clause.
$query->whereNotExists(function($query) {
$query->select('*')->from('orders')->whereColumn('orders.user_id', 'users.id');
});whereRaw($sql, $bindings = [], $boolean = 'and')
Add a raw where clause.
$query->whereRaw('votes > 100');
$query->whereRaw('votes > ? AND created_at > ?', [100, '2024-01-01']);orWhereRaw($sql, $bindings = [])
Add an OR raw where clause.
$query->orWhereRaw('votes > 100');whereDate($column, $operator, $value = null, $boolean = 'and')
Add a where date clause.
$query->whereDate('created_at', '2024-01-01');
$query->whereDate('created_at', '>', '2024-01-01');whereTime($column, $operator, $value = null, $boolean = 'and')
Add a where time clause.
$query->whereTime('created_at', '12:30:00');
$query->whereTime('created_at', '>=', '09:00:00');whereDay($column, $operator, $value = null, $boolean = 'and')
Add a where day clause.
$query->whereDay('created_at', '25');
$query->whereDay('created_at', '>', '15');whereMonth($column, $operator, $value = null, $boolean = 'and')
Add a where month clause.
$query->whereMonth('created_at', '12');
$query->whereMonth('created_at', '<', '6');whereYear($column, $operator, $value = null, $boolean = 'and')
Add a where year clause.
$query->whereYear('created_at', '2024');
$query->whereYear('created_at', '>=', '2020');whereColumn($first, $operator = null, $second = null, $boolean = 'and')
Add a where column clause comparing two columns.
$query->whereColumn('first_name', 'last_name');
$query->whereColumn('updated_at', '>', 'created_at');
$query->whereColumn([
['first_name', '=', 'last_name'],
['updated_at', '>', 'created_at']
]);orWhereColumn($first, $operator = null, $second = null)
Add an OR where column clause.
$query->orWhereColumn('first_name', 'last_name');whereJsonContains($column, $value, $boolean = 'and', $not = false)
Add a where JSON contains clause (MySQL/PostgreSQL).
$query->whereJsonContains('options->languages', 'en');
$query->whereJsonContains('options->languages', ['en', 'fr']);whereJsonLength($column, $operator, $value = null, $boolean = 'and')
Add a where JSON length clause (MySQL/PostgreSQL).
$query->whereJsonLength('options->languages', 2);
$query->whereJsonLength('options->languages', '>', 1);whereFullText($columns, $value, $boolean = 'and')
Add a full text search clause (MySQL/PostgreSQL).
$query->whereFullText(['title', 'content'], 'Laravel');when($value, callable $callback, ?callable $default = null)
Apply clauses conditionally.
$query->when($request->has('filter'), function($query) use ($request) {
return $query->where('status', $request->filter);
});
$query->when(
$sortBy,
function($query, $value) {
return $query->orderBy($value);
},
function($query) {
return $query->orderBy('created_at');
}
);unless($value, callable $callback, ?callable $default = null)
Apply clauses unless a condition is true.
$query->unless($includeDeleted, function($query) {
return $query->whereNull('deleted_at');
});Joins
join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false)
Add an inner join.
$query->join('contacts', 'users.id', '=', 'contacts.user_id');
$query->join('contacts', function($join) {
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.type', '=', 'primary');
});leftJoin($table, $first, $operator = null, $second = null)
Add a left join.
$query->leftJoin('posts', 'users.id', '=', 'posts.user_id');rightJoin($table, $first, $operator = null, $second = null)
Add a right join.
$query->rightJoin('posts', 'users.id', '=', 'posts.user_id');crossJoin($table, $first = null, $operator = null, $second = null)
Add a cross join.
$query->crossJoin('colors');
$query->crossJoin('sizes', 'sizes.id', '>', 'colors.id');joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner')
Join to a subquery.
$subquery = $connection->table('posts')
->select('user_id', $connection->raw('MAX(created_at) as last_post'))
->groupBy('user_id');
$query->joinSub($subquery, 'latest', 'users.id', '=', 'latest.user_id');leftJoinSub($query, $as, $first, $operator = null, $second = null)
Left join to a subquery.
$query->leftJoinSub($subquery, 'latest', 'users.id', '=', 'latest.user_id');Ordering, Grouping & Limiting
orderBy($column, $direction = 'asc')
Add an order by clause.
$query->orderBy('name');
$query->orderBy('created_at', 'desc');orderByDesc($column)
Add a descending order by clause.
$query->orderByDesc('created_at');orderByRaw($sql, $bindings = [])
Add a raw order by clause.
$query->orderByRaw('FIELD(status, ?, ?, ?)', ['pending', 'active', 'completed']);latest($column = 'created_at')
Order by the latest date.
$query->latest();
$query->latest('updated_at');oldest($column = 'created_at')
Order by the oldest date.
$query->oldest();
$query->oldest('updated_at');inRandomOrder($seed = '')
Order results randomly.
$query->inRandomOrder();
$query->inRandomOrder('12345'); // With seed for consistencygroupBy(...$groups)
Add a group by clause.
$query->groupBy('status');
$query->groupBy('status', 'type');having($column, $operator = null, $value = null, $boolean = 'and')
Add a having clause.
$query->groupBy('status')->having('COUNT(*)', '>', 5);orHaving($column, $operator = null, $value = null)
Add an OR having clause.
$query->orHaving('SUM(amount)', '>', 1000);havingRaw($sql, $bindings = [], $boolean = 'and')
Add a raw having clause.
$query->havingRaw('SUM(amount) > ?', [1000]);limit($value)
Set the limit.
$query->limit(10);take($value)
Alias for limit.
$query->take(10);offset($value)
Set the offset.
$query->offset(20);skip($value)
Alias for offset.
$query->skip(20);page($page, $perPage = 15)
Set limit and offset for pagination.
$query->page(2, 20); // Page 2 with 20 items per pageAggregates
count($columns = '*')
Get the count of results.
$count = $query->count();
$count = $query->count('id');min($column)
Get the minimum value.
$min = $query->min('price');max($column)
Get the maximum value.
$max = $query->max('price');sum($column)
Get the sum of values.
$sum = $query->sum('amount');avg($column)
Get the average value.
$avg = $query->avg('rating');average($column)
Alias for avg.
$average = $query->average('rating');exists()
Determine if any results exist.
if ($query->exists()) {
// Records exist
}doesntExist()
Determine if no results exist.
if ($query->doesntExist()) {
// No records exist
}Retrieving Results
get($columns = ['*'])
Execute the query and get all results.
$users = $query->get();
$users = $query->get(['id', 'name']);
foreach ($users as $user) {
echo $user->name;
}first($columns = ['*'])
Get the first result.
$user = $query->first();
if ($user) {
echo $user->name;
}find($id, $columns = ['*'])
Find a record by ID.
$user = $query->find(1);value($column)
Get a single column value from the first result.
$email = $query->where('id', 1)->value('email');pluck($column, $key = null)
Get an array of column values.
$names = $query->pluck('name');
// ['John', 'Jane', 'Bob']
$names = $query->pluck('name', 'id');
// [1 => 'John', 2 => 'Jane', 3 => 'Bob']chunk($count, callable $callback)
Process results in chunks.
$query->chunk(100, function($users) {
foreach ($users as $user) {
// Process user
}
// Return false to stop chunking
if (someCondition()) {
return false;
}
});cursor()
Get a generator for memory-efficient iteration.
foreach ($query->cursor() as $user) {
// Process one user at a time
}Insert Statements
insert(array $values)
Insert records.
// Single record
$query->insert([
'name' => 'John',
'email' => 'john@example.com'
]);
// Multiple records
$query->insert([
['name' => 'John', 'email' => 'john@example.com'],
['name' => 'Jane', 'email' => 'jane@example.com']
]);insertGetId(array $values, $sequence = null)
Insert and get the ID.
$id = $query->insertGetId([
'name' => 'John',
'email' => 'john@example.com'
]);insertOrIgnore(array $values)
Insert records, ignoring duplicates.
$query->insertOrIgnore([
'id' => 1,
'email' => 'john@example.com'
]);Update Statements
update(array $values)
Update records.
$affected = $query->where('id', 1)->update([
'name' => 'Jane',
'updated_at' => now()
]);updateOrInsert(array $attributes, array $values = [])
Update or insert a record.
$query->updateOrInsert(
['email' => 'john@example.com'],
['name' => 'John Doe', 'votes' => 0]
);increment($column, $amount = 1, array $extra = [])
Increment a column value.
$query->where('id', 1)->increment('votes');
$query->where('id', 1)->increment('votes', 5);
$query->where('id', 1)->increment('votes', 1, ['updated_at' => now()]);decrement($column, $amount = 1, array $extra = [])
Decrement a column value.
$query->where('id', 1)->decrement('votes');
$query->where('id', 1)->decrement('votes', 5);
$query->where('id', 1)->decrement('votes', 1, ['updated_at' => now()]);Delete Statements
delete($id = null)
Delete records.
$deleted = $query->where('votes', '<', 100)->delete();
$deleted = $query->delete(5); // Delete by IDtruncate()
Remove all records from the table.
$query->truncate();Utility Methods
toSql()
Get the SQL representation of the query.
$sql = $query->where('status', 'active')->toSql();
// SELECT * FROM `users` WHERE `status` = ?getBindings()
Get the query bindings.
$bindings = $query->where('status', 'active')->getBindings();
// ['active']dd()
Dump the query and die.
$query->where('status', 'active')->dd();dump()
Dump the query and continue.
$query->where('status', 'active')->dump()->get();clone()
Clone the query builder instance.
$baseQuery = $connection->table('users')->where('status', 'active');
$admins = $baseQuery->clone()->where('role', 'admin')->get();
$users = $baseQuery->clone()->where('role', 'user')->get();newQuery()
Get a new query builder instance.
$newQuery = $query->newQuery();getConnection()
Get the database connection instance.
$connection = $query->getConnection();raw($value)
Create a raw expression.
$query->select($query->raw('COUNT(*) as total'));Macros and Extensions
Bob Query Builder supports macros for extending functionality:
use Bob\Query\Builder;
// Register a macro
Builder::macro('whereActive', function() {
return $this->where('status', 'active');
});
// Use the macro
$activeUsers = $connection->table('users')->whereActive()->get();
// Register multiple macros
Builder::mixin([
'whereActive' => function() {
return $this->where('status', 'active');
},
'whereInactive' => function() {
return $this->where('status', 'inactive');
}
]);Error Handling
use Bob\Exceptions\QueryException;
use Bob\Exceptions\ConnectionException;
try {
$results = $connection->table('users')->get();
} catch (ConnectionException $e) {
// Handle connection errors
echo "Connection failed: " . $e->getMessage();
} catch (QueryException $e) {
// Handle query errors
echo "Query failed: " . $e->getMessage();
echo "SQL: " . $e->getSql();
echo "Bindings: " . print_r($e->getBindings(), true);
}Query Logging
// Enable query logging
$connection->enableQueryLog();
// Run queries
$connection->table('users')->get();
// Get the query log
$queries = $connection->getQueryLog();
foreach ($queries as $query) {
echo "Query: " . $query['query'] . "\n";
echo "Bindings: " . print_r($query['bindings'], true) . "\n";
echo "Time: " . $query['time'] . "ms\n";
}
// Disable query logging
$connection->disableQueryLog();
// Clear the query log
$connection->flushQueryLog();Database Transactions
// Using closures (recommended)
$connection->transaction(function () use ($connection) {
$connection->table('users')->update(['votes' => 1]);
$connection->table('posts')->delete();
});
// Manual transaction control
$connection->beginTransaction();
try {
$connection->table('users')->update(['votes' => 1]);
$connection->table('posts')->delete();
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
throw $e;
}
// Transaction with attempts
$connection->transaction(function () use ($connection) {
$connection->table('users')->update(['votes' => 1]);
$connection->table('posts')->delete();
}, 5); // Retry up to 5 times