PHPackages                             castroitalo/echoquery - 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. castroitalo/echoquery

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

castroitalo/echoquery
=====================

EchoQuery facilitates the writing of SQL queries inside the PHP code

v1.1.1(1y ago)3299[1 issues](https://github.com/castroitalo/echo-query/issues)MITPHPPHP ^8.2

Since Apr 21Pushed 1y ago1 watchersCompare

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

READMEChangelog (4)Dependencies (2)Versions (5)Used By (0)

EchoQuery
=========

[](#echoquery)

EchoQuery offers a streamlined, intuitive interface for developers. It simplifies complex SQL scripting, enhances readability, and accelerates development, making database interactions effortless and efficient for projects of any scale.

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

[](#basic-usage)

To be able to use the EchoQuery package you need to install it with the following command:

```
composer require castroitalo/echoquery
```

To generate a string with your query using EchoQuery just use the methods available:

- Query example:

```
SELECT
    column_one AS co,
    column_two
FROM
    table_one AS to
WHERE
    column_one = 2
    AND column_two = 5;
```

- PHP code with EchoQuery example:

```
// Importing library
use CastroItalo\EchoQuery\Builder;

// Instanciate class
$echo_query = new Builder();
$query = $echo_query->select(
    ['column_one', 'co'],
    ['column_two']
)
    ->from('table_one', 'to')
    ->where('column_one')
    ->equalsTo(2)
    ->and('column_two')
    ->equalsTo(5)
    ->getQuery();
```

Features
--------

[](#features)

### Select and From Statement

[](#select-and-from-statement)

The base of any SQL query is the SELECT and FROM statement, you can easily do it with echo query:

Let's create this SQL code into PHP using EchoQuery:

```
SELECT column_one
FROM table_one
```

```
use CastroItalo\EchoQuery\Builder;

$query = (new Builder())->select(
    ['column_one']
)
    ->from('table_one')
    ->getQuery();
```

Let's make it more simple:

```
SELECT *
FROM table_one
```

```
use CastroItalo\EchoQuery\Builder;

$query = (new Builder())->select(
    ['*']
)
    ->from('table_one')
    ->getQuery();
```

In this case, every array passed in **select** method, is a column and its alias, you can pass as much columns as you want:

```
use CastroItalo\EchoQuery\Builder;

$query = (new Builder())->select(
    ['column_one', 'co'],
    ['column_two', 'ct'],
    ['column_three', 'ctr']
)
    ->from('table_one')
    ->getQuery();
```

### Where statement

[](#where-statement)

You create each WHERE condition at time, if you want to take the `column_one` data only the data that is greater than 10 you just do:

```
use CastroItalo\EchoQuery\Builder;

$query = (new Builder())->select(
    ['column_one', 'co'],
    ['column_two', 'ct'],
    ['column_three', 'ctr']
)
    ->from('table_one')
    ->where('column_on')
    ->greaterThan(10)
    ->getQuery();
```

You can use `->where()` method with:

- Comparison operators:

    - `->equalsTo(mixed $value): Builder`
    - `->notEqualsTo(mixed $value, string $notEqualsToOperator = '!='): Builder`
    - `->lessThan(mixed $value): Builder`
    - `->lessThanEqualsTo(mixed $value): Builder`
    - `->greaterThan(mixed $value): Builder`
    - `->greaterThanEqualsTo(mixed $value): Builder`
- Logical operators:

    - `->and(string $columnName): Builder`
    - `->or(string $columnName): Builder`
    - `->not(string $columnName): Builder`
- Pattern matching:

    - `->like(string $pattern): Builder`
    - `->notLike(string $pattern): Builder`
- Range conditions:

    - `->between(mixed $start, mixed $end): Builder`
    - `->notBetween(mixed $start, mixed $end): Builder`
- List conditions:

    - `->in(array $list): Builder`
    - `->notIn(array $list): Builder`
- Null conditions:

    - `->isNull(): Builder`
    - `->isNotNull(): Builder`

### JOINS

[](#joins)

To use joins you need to call the JOIN method you want, and specify the table and the JOIN columns like in:

```
SELECT a.column_one AS co,
    b.column_two AS ct
FROM table_one AS a
WHERE column_one > 10
    INNER JOIN table_two AS b
        ON a.column_one = b.column_one
```

```
use CastroItalo\EchoQuery\Builder;

$query = (new Builder())->select(
    ['a.column_one', 'co'],
    ['b.column_two', 'ct'],
)
    ->from('table_one', 'a')
    ->where('column_one')
    ->greaterThan(10)
    ->innerJoin(
        ['table_two', 'b'],
        ['a.column_one', 'b.column_one']
    )
    ->getQuery();
```

To use JOIN with sub query you just need to use the equivalent sub query JOIN method:

```
SELECT a.column_one AS co,
    b.column_two AS ct
FROM table_one AS a
WHERE a.column_one > 10
    INNER JOIN (
        SELECT column_one,
            column_two
        FROM table_two
    ) AS b
        ON a.column_one = b.column_one
```

```
use CastroItalo\EchoQuery\Builder;

$sub_query = (new Builder())->select(
    ['a.column_one', 'co'],
    ['b.column_two', 'ct'],
)
    ->from('table_one', 'a')
    ->where('column_one')
    ->getQuery();
$query = (new Builder())->select(
    ['a.column_one', 'co'],
    ['b.column_two', 'ct'],
)
    ->from('table_one', 'a')
    ->where('column_one')
    ->greaterThan(10)
    ->innerJoin(
        [$sub_query, 'b'],
        ['a.column_one', 'b.column_one']
    )
    ->getQuery();
```

- INNER JOIN:

    - `->innerJoin(array ...$joinInfo): Builder`
    - `->innerJoinSub(array ...$joinInfo): Builder`
- LEFT JOIN:

    - `->leftJoin(array ...$joinInfo): Builder`
    - `->leftJoinSub(array ...$joinInfo): Builder`
- RIGHT JOIN:

    - `->rightJoin(array ...$joinInfo): Builder`
    - `->rightJoinSub(array ...$joinInfo): Builder`
- FULL JOIN:

    - `->fullJoin(array ...$joinInfo): Builder`
    - `->fullJoinSub(array ...$joinInfo): Builder`
- CROSS JOIN:

    - `->crossJoin(array ...$joinInfo): Builder`
    - `->crossJoinSub(array ...$joinInfo): Builder`
- SELF JOIN:

    - `->selfJoin(array ...$joinInfo): Builder`
    - `->selfJoinSub(array ...$joinInfo): Builder`
- NATURAL JOIN:

    - `->naturalJoin(array ...$joinInfo): Builder`
    - `->naturalJoinSub(array ...$joinInfo): Builder`

### UNIONS

[](#unions)

To use use UNIONS just use the `->union(string $unionQuery): Builder` or `->unionAll(string $unionQuery): Builder` with the subsequent query as the parameter:

```
SELECT column_one AS co,
    column_two AS ct,
    column_three AS ctr
FROM table_one AS to
WHERE column_one > 10
UNION
SELECT column_four AS cfr,
    column_five AS cf,
    column_six AS cs
FROM table_two AS tt
WHERE column_five NOT IN (1, 3, 4, 6);
```

```
use CastroItalo\EchoQuery\Builder;

$union_query = (new Builder())->select(
    ['column_four', 'cfr'],
    ['column_five', 'cf'],
    ['column_six', 'cs']
)
    ->from('table_two', 'tt')
    ->where('column_five')
    ->notIn([1, 3, 4, 6])
    ->getQuery();
$query = (new Builder())->select(
    ['column_one', 'co'],
    ['column_two', 'ct'],
    ['column_three', 'ctr']
)
    ->from('table_one', 'to')
    ->where('column_one')
    ->greaterThan(10)
    ->union($union_query)
    ->getQuery();
```

### GROUP BY

[](#group-by)

To use GROUP BY on aggregate functions simply use the `->groupBy(string ...$columns): Builder` method, passing one or more columns to group:

```
SELECT COUNT(column_one) AS co
    SUM(column_two) AS ct
FROM table_one AS to
GROUP BY column_one, column_two
```

```
use CastroItalo\EchoQuery\Builder;

$query = (new Builder())->select(
    ['COUNT(column_one)', 'co'],
    ['SUM(column_two)', 'ct']
)
    ->from('table_one', 'to')
    ->groupBy('column_one', 'column_two')
    ->getQuery();
```

### ORDER BY

[](#order-by)

To use ORDER BY on columns simply use the `->orderBy(array ...$columns): Builder` method, passing one or more columns with the respective ordering function.

```
SELECT column_one AS co,
    column_two AS ct
FROM table_one AS to
ORDER BY column_one,
    column_two DESC
```

```
use CastroItalo\EchoQuery\Builder;

$query = (new Builder())->select(
    ['column_one', 'co'],
    ['column_two', 'ct']
)
    ->from('table_one', 'to')
    ->orderBy(
        ['column_one'],
        ['column_two', 'desc']
    )
    ->getQuery();
```

### HAVING

[](#having)

For using HAVING just use the `->having(string $having): Builder` with one of the comparison operator:

```
SELECT COUNT(column_one) AS co,
    column_two AS ct,
    column_three AS ctr
FROM table_one AS to
WHERE column_one > 10
HAVING COUNT(column_one) > 10
```

```
use CastroItalo\EchoQuery\Builder;

$query = (new Builder())->select(
    ['COUNT(column_one)', 'co'],
    ['column_two', 'ct'],
    ['column_three', 'ctr']
)
    ->from('table_one', 'to')
    ->where('column_one')
    ->greaterThan(10)
    ->having('COUNT(column_one)')
    ->greaterThan(10)
    ->getQuery();
```

### Pagination

[](#pagination)

To create a pagination just use the `->pagination(int $limit, ?int $offset = null): Builder` method, passing the limit and an optional offset for it:

```
SELECT column_one AS co,
    column_two AS ct
FROM table_one AS to
LIMIT 10
```

```
use CastroItalo\EchoQuery\Builder;

$query = (new Builder())->select(
    ['COUNT(column_one)', 'co'],
    ['column_two', 'ct'],
)
    ->from('table_one', 'to')
    ->where('column_one')
    ->pagination(10)
    ->getQuery();
```

Contributing
------------

[](#contributing)

To contribute to the project make sure you have read [CONTRIBUTING](https://github.com/castroitalo/echo-query/blob/main/CONTRIBUTING.md) section.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing-1)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

Credits
-------

[](#credits)

- [Italo Castro](https://github.com/castroitalo)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 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 ~22 days

Total

4

Last Release

678d ago

### Community

Maintainers

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

---

Top Contributors

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

---

Tags

sqlquerybuilder

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/castroitalo-echoquery/health.svg)

```
[![Health](https://phpackages.com/badges/castroitalo-echoquery/health.svg)](https://phpackages.com/packages/castroitalo-echoquery)
```

###  Alternatives

[nilportugues/sql-query-builder

An elegant lightweight and efficient SQL QueryInterface BuilderInterface supporting bindings and complicated query generation.

425239.4k6](/packages/nilportugues-sql-query-builder)[izniburak/pdox

Useful Query Builder, PDO Class for PHP. A simple access to your SQL records.

30221.1k7](/packages/izniburak-pdox)[sad_spirit/pg_builder

Query builder for Postgres backed by SQL parser

5933.6k1](/packages/sad-spirit-pg-builder)[bentools/where

PHP7.1 Fluent, immutable SQL query builder. Connectionless, framework-agnostic, no dependency.

125.2k2](/packages/bentools-where)

PHPackages © 2026

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