PHPackages                             angel-source-labs/laravel-expression-grammar - 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. angel-source-labs/laravel-expression-grammar

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

angel-source-labs/laravel-expression-grammar
============================================

Database Grammar Helper for Laravel. Returns appropriate SQL based on driver for database connection.

v1.1(1y ago)014.1k↑114.3%2MITPHPPHP &gt;=7.1.3

Since Jan 28Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Angel-Source-Labs/laravel-expression-grammar)[ Packagist](https://packagist.org/packages/angel-source-labs/laravel-expression-grammar)[ RSS](/packages/angel-source-labs-laravel-expression-grammar/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (7)Versions (4)Used By (2)

ExpressionGrammar
=================

[](#expressiongrammar)

Grammar helper for providing expressions with grammar differences by database
-----------------------------------------------------------------------------

[](#grammar-helper-for-providing-expressions-with-grammar-differences-by-database)

Sometimes SQL expressions need to provide different grammar for different databases and for different versions of databases.

This package provides an `ExpressionGrammar` class that will produce the appropriate expression for the database and version in use.

For example, when [Excel Seeder for Laravel](https://github.com/bfinlay/laravel-excel-seeder) tests exception messages, it can resolve the grammar based on the database used:

```
        $this->expectExceptionMessage(ExpressionGrammar::make()
            ->sqLite('Integrity constraint violation: 19 FOREIGN KEY constraint failed')
            ->mySql('Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint')
            ->sqlServer('Cannot truncate table \'users\' because it is being referenced by a FOREIGN KEY constraint.')
        );
        \DB::table('users')->truncate();
```

As another example, when working with `ST_GeomFromText()` between MySQL 8.0 vs MySQL 5.7 and Postgres, the order of latitude and longitude is different, and when switching between databases you might want your code base to work the same without changes. MySQL 8.0 provides an option for `ST_GeomFromText()` to change the axis order. So while the grammar for Postgres will look like `ST_GeomFromText('POINT(1 2)', 4326)`, the grammar for MySql 8.0 will look like `ST_GeomFromText('POINT(1 2)', 4326, 'axis-order=long-lat')`.

Creating an `Expression` with an `ExpressionGrammar` to support these three different grammars would look like this:

```
$expression = ExpressionGrammar::make()
        ->mySql("ST_GeomFromText('POINT(1 2)', 4326)")
        ->mySql("ST_GeomFromText('POINT(1 2)', 4326, 'axis-order=long-lat')", "8.0")
        ->postgres("ST_GeomFromText('POINT(1 2)', 4326)");
```

This will resolve to the following expressions for the specified databases and versions:

databaseversionresultMySQLdefaultST\_GeomFromText('POINT(1 2)', 4326)MySQL8.0 and higherST\_GeomFromText('POINT(1 2)', 4326, 'axis-order=long-lat')PostgresdefaultST\_GeomFromText('POINT(1 2)', 4326)Laravel Expressions do not support bindings by default. [Expressions for Laravel](https://github.com/Angel-Source-Labs/laravel-expressions) is a package that provides Expressions that support bindings, which allows the ExpressionGrammar helper to return Expressions with bindings that will be evaluated by the query builder.

### Available Methods

[](#available-methods)

The `ExpressionGrammar` class provides a fluent interface for adding grammar expressions and has methods for each built-in Laravel driver as well as a generic `grammar` method that allows specifying a driver string for other databases.

#### \#`ExpressionGrammar::make()`

[](#expressiongrammarmake)

Creates a new Grammar instance and provides a fluent interface for adding grammar expressions.

#### \#`ExpressionGrammar->mySql($string, $version (optional))`

[](#expressiongrammar-mysqlstring-version-optional)

Add an expression for MySQL grammar.

#### \#`ExpressionGrammar->postgres($string, $version (optional))`

[](#expressiongrammar-postgresstring-version-optional)

Add an expression for Postgres grammar.

#### \#`ExpressionGrammar->sqLite($string, $version (optional))`

[](#expressiongrammar-sqlitestring-version-optional)

Add an expression for SQLite grammar.

#### \#`ExpressionGrammar->sqlServer($string, $version (optional))`

[](#expressiongrammar-sqlserverstring-version-optional)

Add an expression for SqlServer grammar.

#### \#`ExpressionGrammar->grammar($driver, $string, $version (optional))`

[](#expressiongrammar-grammardriver-string-version-optional)

Add an expression for grammar for other database drivers. `$driver` should match the driver string used by the Laravel query builder driver. For example `$grammar->postgres("ST_GeomFromText('POINT(1 2)', 4326)")` is equivalent to `$grammar->grammar("pgsql", "ST_GeomFromText('POINT(1 2)', 4326)")`.

The `$version` parameter is optional. When not specified, the grammar applies as the default. When specified, the grammar applies to the specified version of the database or greater.

`ExpressionGrammar` will throw a `GrammarNotDefinedForDatabaseException` if the Query Builder attempts to resolve an Expression for a Grammar that has not been defined for that database driver.

License
-------

[](#license)

ExpresionGrammar for Laravel is open-sourced software licensed under the MIT license.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity42

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

Total

2

Last Release

594d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/75f4790522212e9f5588334a23ab3e4199ea8a465773eb8d7796b84b7055ff26?d=identicon)[bfinlay](/maintainers/bfinlay)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/angel-source-labs-laravel-expression-grammar/health.svg)

```
[![Health](https://phpackages.com/badges/angel-source-labs-laravel-expression-grammar/health.svg)](https://phpackages.com/packages/angel-source-labs-laravel-expression-grammar)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11120.2M21](/packages/anourvalar-eloquent-serialize)[overtrue/laravel-versionable

Make Laravel model versionable.

585308.0k5](/packages/overtrue-laravel-versionable)[abbasudo/laravel-purity

elegant way to add filter and sort in laravel

514330.5k1](/packages/abbasudo-laravel-purity)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)[dragon-code/laravel-deploy-operations

Performing any actions during the deployment process

240173.5k2](/packages/dragon-code-laravel-deploy-operations)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

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