PHPackages                             soophiiaaa/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. soophiiaaa/query-builder

ActiveLibrary

soophiiaaa/query-builder
========================

A fluent SQL query builder designed as a reusable component for future projects.

v0.1.6(5mo ago)11MITPHPPHP &gt;=8.0

Since Dec 13Pushed 5mo agoCompare

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

READMEChangelogDependencies (2)Versions (7)Used By (0)

💾🔍 Query Builder
================

[](#-query-builder)

[![status](https://camo.githubusercontent.com/583cfcca214a0894fd924c319a3e209499a5da8bc22ab3cb868d82e8cb4a78c4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617475732d616374697665253230646576656c6f706d656e742d626c7565)](https://camo.githubusercontent.com/583cfcca214a0894fd924c319a3e209499a5da8bc22ab3cb868d82e8cb4a78c4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374617475732d616374697665253230646576656c6f706d656e742d626c7565)[![updates](https://camo.githubusercontent.com/e29756748053f22fdb2812afd8747c72cfb0e3a7ef22874a7ac2d608644b7733/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f757064617465732d6672657175656e742d627269676874677265656e)](https://camo.githubusercontent.com/e29756748053f22fdb2812afd8747c72cfb0e3a7ef22874a7ac2d608644b7733/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f757064617465732d6672657175656e742d627269676874677265656e)[![tests](https://camo.githubusercontent.com/4b0fce50a2b20b61b90254c8578b05005c4357f46e1ab3065a07f724697c598c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d696e25323070726f67726573732d79656c6c6f77)](https://camo.githubusercontent.com/4b0fce50a2b20b61b90254c8578b05005c4357f46e1ab3065a07f724697c598c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d696e25323070726f67726573732d79656c6c6f77)

This repository is a query builder — a tool that allows you to create database queries programmatically using methods and functions instead of writing raw SQL. This makes the code more readable, secure, flexible, and easier to maintain, almost like assembling building blocks to construct statements such as `SELECT`, `WHERE`, and `ORDER BY` in an intuitive way.

The development of this component has been a major learning experience. I'm improving my programming logic, adopting better practices, and gaining a deeper understanding of software architecture, testing, and security.

This project is not an ORM and does not manage entities or persistence.

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

[](#installation)

### Requirements

[](#requirements)

- PHP 8.0+
- Composer
- PostgreSQL
- Dotenv library (`vlucas/phpdotenv`)

### 1. Via Composer

[](#1-via-composer)

```
composer require soophiiaaa/query-builder
```

### 2. Environment configuration

[](#2-environment-configuration)

Copy the example env file and fill in your values:

```
cp .env.example .env
```

Edit `.env` and set database credentials, etc:

```
DB_HOST=localhost
DB_PORT=5432
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=your_database

```

How to Use
----------

[](#how-to-use)

Here’s a simple example showing how to build a SQL query using this Query Builder:

> The `get()` method returns the generated SQL string. It does NOT execute the query.

### Select

[](#select)

```
$query = new QueryBuilder();

$sql = $query->select(['id', 'name'])
    ->from('students')
    ->where('age', '>', 18)
    ->limit(10)
    ->get();

echo $sql . "\n";
```

### Insert

[](#insert)

```
$query = new QueryBuilder();

$sql = $query->insert()
    ->into('students')
    ->values(['name' => 'John Doe', 'age' => 20, 'country' => 'Brazil'])
    ->get();

echo $sql . "\n";
```

### Update

[](#update)

```
$query = new QueryBuilder();

$sql = $query->update('students')
    ->set('course', 'Systems Development')
    ->where('id', '=', '2')
    ->get();

echo $sql . "\n";
```

### Delete

[](#delete)

```
$query = new QueryBuilder();

$sql = $query->delete()
    ->from('students')
    ->where('active', '=', false)
    ->get();

echo $sql . "\n";
```

Advanced Filtering (AND / OR)
-----------------------------

[](#advanced-filtering-and--or)

By default, all conditions added with the `where` method are combined using the **AND** operator.

```
$query = new QueryBuilder();

$sql = $query->select()
      ->from('students')
      ->where('age', '>', 18)
      ->where('active', '=', true);
```

To use the **OR** operator, you must pass the `logical` argument to the `where` method. The use of **Named Arguments** (a feature of PHP 8) is recommended for better readability and clarity.

```
$query = new QueryBuilder();

$sql = $query->select('id')
      ->from('users')
      ->where('age', '>', 18)
      ->where(
          variable: 'name',
          operator: 'LIKE',
          value: 'A%',
          logical: 'OR' // get();
```

### Important: Query Execution

[](#important-query-execution)

> **This Query Builder does NOT execute SQL queries.**

Its sole responsibility is to **build SQL strings** in a safe, readable, and structured way using a fluent interface.

The actual execution of the query is **always handled externally**, typically by a `PDO` connection.

Example:

```
$query = new QueryBuilder();
$pdo = Connection::connect();

$sql = $query->select('id')
      ->from('users')
      ->where('age', '>', 18)
      ->where('name', 'LIKE', 'A%', 'OR')
      ->get();

$result = $pdo->query($sql);
```

Credits
-------

[](#credits)

It was truly a challenge to begin building this query builder, as I didn’t even know where to start. I found an excellent book that helped me a lot and became my guide throughout the entire process:

***“PHP Programando com Orientação a Objetos”* by Pablo Dall'Oglio**

The only note is that I read the 2nd Edition, which was written for PHP 5. Even so, it was interesting to adapt the code to a more modern version of the language.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance73

Regular maintenance activity

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity34

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

Every ~0 days

Total

6

Last Release

152d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7c6a27ff3fbc2d0939744f524d3020608ed554c0b16ce9264e902c594a0dfdd5?d=identicon)[soophiiaaa](/maintainers/soophiiaaa)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[bacula-web/bacula-web

The open source web based reporting and monitoring tool for Bacula

1537.5k](/packages/bacula-web-bacula-web)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)[doppar/framework

The Doppar Framework

366.7k8](/packages/doppar-framework)[hardcastle/xrpl_php

PHP SDK / Client for the XRP Ledger

129.7k5](/packages/hardcastle-xrpl-php)

PHPackages © 2026

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