PHPackages                             mysaaspackage/query-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mysaaspackage/query-builder

ActiveLibrary[Database &amp; ORM](/categories/database)

mysaaspackage/query-builder
===========================

Provides a query builder for the database

v0.0.1(1y ago)0106MITPHPPHP &gt;=8.1

Since Apr 13Pushed 1y agoCompare

[ Source](https://github.com/MySaasPackage/QueryBuilder)[ Packagist](https://packagist.org/packages/mysaaspackage/query-builder)[ RSS](/packages/mysaaspackage-query-builder/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

QueryBuilder
============

[](#querybuilder)

A fluent SQL query builder for PHP that supports complex queries including CTEs, recursive CTEs, and various SQL operations.

Installation
------------

[](#installation)

```
composer require mysaaspackage/querybuilder
```

Basic Usage
-----------

[](#basic-usage)

### SELECT Queries

[](#select-queries)

```
use MySaasPackage\QueryBuilder\QueryBuilder;

// Basic select
$query = new QueryBuilder('users');
$query->select('id', 'name', 'email');
// SELECT id, name, email FROM users

// Select with where clause
$query = new QueryBuilder('users');
$query->select('*')
    ->where('age > :age', ['age' => 18]);
// SELECT * FROM users WHERE age > :age

// Select with multiple conditions
$query = new QueryBuilder('users');
$query->select('*')
    ->where('age > :age', ['age' => 18])
    ->andWhere('status = :status', ['status' => 'active'])
    ->orWhere('is_admin = :is_admin', ['is_admin' => true]);
// SELECT * FROM users WHERE age > :age AND status = :status OR is_admin = :is_admin

// Select with joins
$query = new QueryBuilder('users');
$query->select('users.*', 'profiles.bio')
    ->join('profiles', 'p', 'users.id = p.user_id')
    ->leftJoin('addresses', 'a', 'users.id = a.user_id');
// SELECT users.*, profiles.bio FROM users JOIN profiles AS p ON users.id = p.user_id LEFT JOIN addresses AS a ON users.id = a.user_id

// Select with group by and having
$query = new QueryBuilder('orders');
$query->select('user_id', 'COUNT(*) as order_count')
    ->groupBy('user_id')
    ->having('COUNT(*) > :min_orders', ['min_orders' => 5]);
// SELECT user_id, COUNT(*) as order_count FROM orders GROUP BY user_id HAVING COUNT(*) > :min_orders

// Select with order by and limit
$query = new QueryBuilder('products');
$query->select('*')
    ->orderBy('price', 'DESC')
    ->orderBy('name')
    ->limit(10)
    ->offset(20);
// SELECT * FROM products ORDER BY price DESC, name LIMIT 10 OFFSET 20
```

### Common Table Expressions (CTEs)

[](#common-table-expressions-ctes)

```
// Basic CTE
$subQuery = new QueryBuilder('orders');
$subQuery->select('user_id', 'SUM(amount) as total_amount')
    ->groupBy('user_id');

$query = new QueryBuilder('users');
$query->with('user_totals', $subQuery)
    ->select('users.*', 'user_totals.total_amount')
    ->join('user_totals', 'ut', 'users.id = ut.user_id');
// WITH user_totals AS (SELECT user_id, SUM(amount) as total_amount FROM orders GROUP BY user_id)
// SELECT users.*, user_totals.total_amount FROM users JOIN user_totals AS ut ON users.id = ut.user_id

// Recursive CTE (for hierarchical data)
$baseQuery = new QueryBuilder('categories');
$baseQuery->select('id', 'name', 'parent_id')
    ->where('parent_id IS NULL');

$recursiveQuery = new QueryBuilder();
$recursiveQuery->select('c.id', 'c.name', 'c.parent_id')
    ->from('categories', 'c')
    ->join('category_tree', 'ct', 'c.parent_id = ct.id');

$query = new QueryBuilder();
$query->withRecursive('category_tree', $baseQuery, $recursiveQuery)
    ->select('*')
    ->from('category_tree');
// WITH RECURSIVE category_tree AS (
//     SELECT id, name, parent_id FROM categories WHERE parent_id IS NULL
//     UNION ALL
//     SELECT c.id, c.name, c.parent_id FROM categories AS c
//     JOIN category_tree AS ct ON c.parent_id = ct.id
// )
// SELECT * FROM category_tree
```

### INSERT, UPDATE, and DELETE Operations

[](#insert-update-and-delete-operations)

```
// INSERT
$query = new QueryBuilder();
$query->insert('users')
    ->values([
        'name' => ':name',
        'email' => ':email',
        'age' => ':age'
    ])
    ->setParameter('name', 'John Doe')
    ->setParameter('email', 'john@example.com')
    ->setParameter('age', 30);
// INSERT INTO users (name, email, age) VALUES (:name, :email, :age)

// UPDATE
$query = new QueryBuilder();
$query->update('users')
    ->set([
        'name' => ':new_name',
        'email' => ':new_email'
    ])
    ->where('id = :id', ['id' => 1])
    ->setParameter('new_name', 'Jane Doe')
    ->setParameter('new_email', 'jane@example.com');
// UPDATE users SET name = :new_name, email = :new_email WHERE id = :id

// DELETE
$query = new QueryBuilder();
$query->delete('users')
    ->where('id = :id', ['id' => 1]);
// DELETE FROM users WHERE id = :id
```

### Complex Queries with Subqueries

[](#complex-queries-with-subqueries)

```
// Subquery in WHERE clause
$subQuery = new QueryBuilder('orders');
$subQuery->select('user_id')
    ->where('total > :min_total', ['min_total' => 1000]);

$query = new QueryBuilder('users');
$query->select('*')
    ->where('id IN :user_ids', ['user_ids' => $subQuery]);
// SELECT * FROM users WHERE id IN (SELECT user_id FROM orders WHERE total > :min_total)

// Subquery in SELECT clause
$avgQuery = new QueryBuilder('products');
$avgQuery->select('AVG(price)');

$query = new QueryBuilder('products');
$query->select('name', 'price')
    ->where('price > :avg_price', ['avg_price' => $avgQuery]);
// SELECT name, price FROM products WHERE price > (SELECT AVG(price) FROM products)
```

API Reference
-------------

[](#api-reference)

### Query Construction Methods

[](#query-construction-methods)

- `select(...$columns): self` - Set columns to select
- `from(string $table, ?string $alias = null): self` - Set the main table
- `where(string $condition, array $params = []): self` - Add a WHERE condition
- `andWhere(string $condition, array $params = []): self` - Add an AND WHERE condition
- `orWhere(string $condition, array $params = []): self` - Add an OR WHERE condition
- `join(string $table, string $alias, string $condition): self` - Add an INNER JOIN
- `leftJoin(string $table, string $alias, string $condition): self` - Add a LEFT JOIN
- `rightJoin(string $table, string $alias, string $condition): self` - Add a RIGHT JOIN
- `groupBy(string ...$columns): self` - Add GROUP BY clauses
- `having(string $condition, array $params = []): self` - Add a HAVING condition
- `orderBy(string $column, ?string $direction = null): self` - Add an ORDER BY clause
- `limit(int $limit): self` - Set the LIMIT
- `offset(int $offset): self` - Set the OFFSET

### CTE Methods

[](#cte-methods)

- `with(string $name, QueryBuilder $query, array $columns = [], bool $recursive = false): self` - Add a CTE
- `withRecursive(string $name, QueryBuilder $baseQuery, QueryBuilder $recursiveQuery, array $columns = []): self` - Add a recursive CTE

### DML Operations

[](#dml-operations)

- `insert(string $table): self` - Start an INSERT query
- `values(array $values): self` - Set values for INSERT
- `update(string $table): self` - Start an UPDATE query
- `set(array $values): self` - Set values for UPDATE
- `delete(string $table): self` - Start a DELETE query

### Parameter Methods

[](#parameter-methods)

- `setParameter(string|int $key, mixed $value): self` - Set a parameter value
- `getParams(): array` - Get all parameters

### Finalization Methods

[](#finalization-methods)

- `toSQL(): string` - Get the final SQL query

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance46

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

400d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e0fa15b075dad687aa52a422c935d9ef734b8768bf3cc8a5d7d4ed70c6d81797?d=identicon)[Alef Castelo](/maintainers/Alef%20Castelo)

---

Top Contributors

[![alefcastelo](https://avatars.githubusercontent.com/u/4504483?v=4)](https://github.com/alefcastelo "alefcastelo (4 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/mysaaspackage-query-builder/health.svg)

```
[![Health](https://phpackages.com/badges/mysaaspackage-query-builder/health.svg)](https://phpackages.com/packages/mysaaspackage-query-builder)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
