PHPackages                             halimzidoune/query-macro-helper - 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. halimzidoune/query-macro-helper

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

halimzidoune/query-macro-helper
===============================

A Laravel package that extends the Query Builder, allowing you to create custom macros to work with different database drivers easily. Instead of using raw DB::raw() statements and checking the current driver each time, this package simplifies working with multiple databases. It makes your code cleaner, easier to read, and more organized, all while being fully compatible with all Laravel-supported database drivers.

v1.0.4(12mo ago)84MITPHPPHP ^8.1

Since Aug 21Pushed 11mo agoCompare

[ Source](https://github.com/halimzidoune/query-macro)[ Packagist](https://packagist.org/packages/halimzidoune/query-macro-helper)[ RSS](/packages/halimzidoune-query-macro-helper/feed)WikiDiscussions main Synced 1w ago

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

Laravel Query Macro Helper
==========================

[](#laravel-query-macro-helper)

[![](https://github.com/halimzidoune/query-macro/raw/main/query_macro.png)](https://github.com/halimzidoune/query-macro/blob/main/query_macro.png)

A Laravel package that extends the Query Builder with database-agnostic SELECT macros. Transform your raw SQL expressions into readable, chainable methods that automatically generate the correct syntax for each database driver. Replace complex DB::raw() statements with intuitive methods like selectConcat(), selectDateFormat(), and selectCase() that work seamlessly across MySQL, SQLite, PostgreSQL, Oracle, SQL Server.

🚀 Features
----------

[](#-features)

- **Database Agnostic**: Automatically adapts SQL syntax for MySQL, PostgreSQL, SQLite, SQL Server, and Oracle
- **Clean API**: Simple, intuitive method names that work like native Laravel methods
- **No Raw SQL**: Eliminate the need for `DB::raw()` statements and driver-specific code
- **Easy Extension**: Create custom macros with the built-in artisan command
- **Laravel 10 &amp; 11 Support**: Compatible with the latest Laravel versions

✨ Benefits
----------

[](#-benefits)

- **Single code path**: One query works across all supported drivers
- **Readable queries**: Intent-revealing `selectX()` methods instead of raw SQL
- **Safer expressions**: Centralize SQL generation and avoid copy/paste errors
- **Easy to extend**: Add your own macros with a tiny class; auto-registered
- **Composable**: Chain multiple macros with standard Query/Eloquent builders
- **Production-ready**: Covers common String, Number, Datetime, and Cast use cases

📦 Installation
--------------

[](#-installation)

### Via Composer

[](#via-composer)

```
composer require halimzidoune/query-macro-helper
```

### Service Provider Registration

[](#service-provider-registration)

Add this to your `config/app.php`:

```
'providers' => [
    // ...
    Hz\QueryMacroHelper\QueryMacroHelperServiceProvider::class,
],
```

For laravel &gt; 10 `bootstrap/providers.php`:

```
return [
    // ...
    Hz\QueryMacroHelper\QueryMacroHelperServiceProvider::class,
],
```

It's recomended to add this line in `AppSeriveProvider@boot` for a better cast:

```
DB::connection()->getPdo()->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
```

✍️ Writing a custom macro
-------------------------

[](#️-writing-a-custom-macro)

Use the generator, then implement driver-aware SQL as needed.

```
php artisan make:macro Lower
```

This creates `app/Builders/Macros/Lower.php`. Example implementation:

```
