├── README.md ├── composer.json ├── phpstan-baseline.neon ├── phpstan.neon.dist ├── phpunit.xml └── src ├── Macros ├── DdQuery.php ├── DumpQuery.php ├── RawQuery.php └── RayQuery.php └── QueryBuilderMacroServiceProvider.php /README.md: -------------------------------------------------------------------------------- 1 | # A set of useful macros for the query builder 2 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/uteq/laravel-query-builder-macros.svg?style=flat-square)](https://packagist.org/packages/uteq/laravel-query-builder-macros) 3 | ![Run tests](https://github.com/uteq/laravel-query-builder-macros/workflows/run-tests/badge.svg) 4 | ![Check & fix styling](https://github.com/uteq/laravel-query-builder-macros/workflows/Check%20&%20fix%20styling/badge.svg) 5 | [![Total Downloads](https://img.shields.io/packagist/dt/uteq/laravel-query-builder-macros.svg?style=flat-square)](https://packagist.org/packages/uteq/laravel-query-builder-macros) 6 | 7 | Adds useful functionality to the Laravel Query Builder 8 | `Illuminate\Database\Query\Builder`. 9 | 10 | ## Installation 11 | 12 | ```bash 13 | composer require uteq/laravel-query-builder-macros 14 | ``` 15 | 16 | Or add the Uteq Laravel Support package 17 | ```bash 18 | composer require uteq/laravel-support 19 | ``` 20 | 21 | ### `rawQuery` 22 | ```php 23 | $rawQuery = Account::query() 24 | ->where('email', 'john') 25 | ->getQuery() // You need to do this to get the rawQuery 26 | ->rawQuery(); 27 | // return `select * from "account" where "email" = 'john'` 28 | ``` 29 | 30 | ### `ddQuery` 31 | ```php 32 | Account::query() 33 | ->where('email', 'john') 34 | ->ddQuery(); 35 | // dd() of `select * from "account" where "email" = 'john'` 36 | ``` 37 | 38 | ### `dumpQuery` 39 | ```php 40 | Account::query() 41 | ->where('email', 'john') 42 | ->dumpQuery(); 43 | // dump() of `select * from "account" where "email" = 'john'` 44 | // the execution of the script will continue 45 | ``` 46 | 47 | 48 | ### `rayQuery` 49 | ```php 50 | Account::query() 51 | ->where('email', 'john') 52 | ->rayQuery(); 53 | // ray() of `select * from "account" where "email" = 'john'` 54 | // the execution of the script will continue 55 | ``` 56 | 57 | ## Inspiration by 58 | Building this package I got inspired by the following: 59 | - https://stackoverflow.com/a/53337416 60 | - http://github.com/spatie/laravel-collection-macros 61 | - https://github.com/spatie/package-skeleton-laravel 62 | 63 | ## Credits 64 | - [Nathan Jansen](https://github.com/nathanjansen) 65 | - [Leo Flapper](https://github.com/leoflapper) 66 | - [All Contributors](../../contributors) 67 | 68 | ## Testing 69 | ``` bash 70 | $ composer test 71 | ``` 72 | 73 | ## About Uteq 74 | We are a web development agency based in the Netherlands. 75 | We design and build web applications for our clients. 76 | We are specialized in Laravel with Livewire. 77 | 78 | ## License 79 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 80 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "uteq/laravel-query-builder-macros", 3 | "description": "A set of common Laravel Uteq helpers and macros", 4 | "keywords": [ 5 | "uteq", 6 | "query-builder-macros" 7 | ], 8 | "homepage": "https://github.com/uteq/query-builder-macros", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Nathan Jansen", 13 | "email": "info@nathanjansen.nl", 14 | "homepage": "https://github.com/nathanjansen", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0", 20 | "illuminate/support": "^8.71|^9.0|^10.0" 21 | }, 22 | "require-dev": { 23 | "laravel/pint": "^1.0", 24 | "nunomaduro/collision": "^7.9", 25 | "nunomaduro/larastan": "^2.0.1", 26 | "orchestra/testbench": "^8.0", 27 | "pestphp/pest": "^2.0", 28 | "pestphp/pest-plugin-arch": "^2.0", 29 | "pestphp/pest-plugin-laravel": "^2.0", 30 | "phpstan/extension-installer": "^1.1", 31 | "phpstan/phpstan-deprecation-rules": "^1.0", 32 | "phpstan/phpstan-phpunit": "^1.0", 33 | "spatie/laravel-ray": "^1.26" 34 | }, 35 | "autoload": { 36 | "psr-4": { 37 | "Uteq\\QueryBuilderMacros\\": "src/" 38 | } 39 | }, 40 | "autoload-dev": { 41 | "psr-4": { 42 | "Uteq\\QueryBuilderMacros\\Tests\\": "tests/" 43 | } 44 | }, 45 | "scripts": { 46 | "post-autoload-dump": "@php ./vendor/bin/testbench package:discover --ansi", 47 | "analyse": "vendor/bin/phpstan analyse", 48 | "test": "vendor/bin/pest", 49 | "test-coverage": "vendor/bin/pest --coverage", 50 | "format": "vendor/bin/pint" 51 | }, 52 | "config": { 53 | "sort-packages": true, 54 | "allow-plugins": { 55 | "pestphp/pest-plugin": true, 56 | "phpstan/extension-installer": true 57 | } 58 | }, 59 | "extra": { 60 | "laravel": { 61 | "providers": [ 62 | "Uteq\\QueryBuilderMacros\\QueryBuilderMacroServiceProvider" 63 | ], 64 | "aliases": { 65 | "QueryBuilderMacros": "Uteq\\QueryBuilderMacros\\Facades\\QueryBuilderMacros" 66 | } 67 | } 68 | }, 69 | "minimum-stability": "dev", 70 | "prefer-stable": true 71 | } 72 | -------------------------------------------------------------------------------- /phpstan-baseline.neon: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uteq/laravel-query-builder-macros/45fabc3d2efe137a7df37150935efd719057f625/phpstan-baseline.neon -------------------------------------------------------------------------------- /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | includes: 2 | - phpstan-baseline.neon 3 | 4 | parameters: 5 | level: 4 6 | paths: 7 | - src 8 | tmpDir: build/phpstan 9 | checkOctaneCompatibility: true 10 | checkModelProperties: true 11 | checkMissingIterableValueType: false 12 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 18 | 19 | 20 | tests 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/Macros/DdQuery.php: -------------------------------------------------------------------------------- 1 | rawQuery()); 14 | }; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Macros/DumpQuery.php: -------------------------------------------------------------------------------- 1 | rawQuery()); 14 | }; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Macros/RawQuery.php: -------------------------------------------------------------------------------- 1 | toSql()), 15 | values: collect($this->getBindings()) 16 | ->map(fn ($binding) => is_numeric($binding) ? $binding : "'{$binding}'") 17 | ->toArray() 18 | ); 19 | }; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Macros/RayQuery.php: -------------------------------------------------------------------------------- 1 | rawQuery()); 14 | }; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/QueryBuilderMacroServiceProvider.php: -------------------------------------------------------------------------------- 1 | macros()) 13 | // Don't register macros that already exist 14 | ->reject(fn ($class, $macro) => Builder::hasMacro($macro)) 15 | // Register the macros 16 | ->each(fn ($class, $macro) => Builder::macro($macro, app($class)())); 17 | } 18 | 19 | private function macros(): array 20 | { 21 | return [ 22 | 'rawQuery' => Macros\RawQuery::class, 23 | 'ddQuery' => Macros\DdQuery::class, 24 | 'dumpQuery' => Macros\DumpQuery::class, 25 | 'rayQuery' => Macros\RayQuery::class, 26 | ]; 27 | } 28 | } 29 | --------------------------------------------------------------------------------