├── LICENSE.md ├── README.md ├── composer.json └── src ├── Concerns ├── EloquentBuilderMacros.php └── QueryBuilderMacros.php ├── Exceptions └── ToRawSqlException.php ├── Services └── BuilderToRawSqlService.php └── ToRawSqlServiceProvider.php /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) pyaesoneaung 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![Cover](https://ik.imagekit.io/pyaesoneaung/github/to-raw-sql/cover.png?updatedAt=1687412017963) 2 | 3 | [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/PyaeSoneAungRgn/to-raw-sql/run-tests.yml?branch=main&label=test)](https://github.com/PyaeSoneAungRgn/to-raw-sql/actions/workflows/run-tests.yml) 4 | [![Packagist Downloads](https://img.shields.io/packagist/dt/pyaesoneaung/to-raw-sql)](https://packagist.org/packages/pyaesoneaung/to-raw-sql) 5 | 6 | # To Raw SQL 7 | 8 | ⚠️ The `toRawSql()` function is included by default in Laravel 10.15.0. You don't need to install this package if your Laravel version is greater than 10.14.1 ⚠️ 9 | 10 | Get raw SQL from Laravel Query Builder and Eloquent Builder 11 | 12 | ## Installation 13 | 14 | ```bash 15 | composer require pyaesoneaung/to-raw-sql 16 | ``` 17 | 18 | ## Usage 19 | 20 | ```php 21 | User::where('votes', '>', 100) 22 | ->orWhere(function ($query) { 23 | $query->where('name', 'Abigail') 24 | ->where('votes', '>', 50); 25 | }) 26 | ->toRawSql(); 27 | 28 | // "select * from `users` where `votes` > 100 or (`name` = 'Abigail' and `votes` > 50)" 29 | ``` 30 | 31 | ```php 32 | DB::table('users') 33 | ->whereBetween('votes', [1, 100]) 34 | ->toRawSql(); 35 | 36 | // "select * from `users` where `votes` between 1 and 100" 37 | ``` 38 | 39 | ## Version History 40 | 41 | - 1.1.3 42 | - do not register the `toRawSql()` macro if the Laravel version is greater than 10.14.1 43 | - 1.1.2 44 | - throw ToRawSqlException when encountering PostgreSQL jsonb operator errors 45 | - 1.1.1 46 | - fixed boolean bind for pgsql 47 | - 1.1.0 48 | - support Illuminate\Database\Query\Builder 49 | - 1.0.2 50 | - support DateTimeInterface bind 51 | - 1.0.1 52 | - fixed string bind 53 | - 1.0.0 54 | - support Illuminate\Database\Eloquent\Builder 55 | 56 | ## Testing 57 | 58 | ```bash 59 | composer test 60 | ``` 61 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pyaesoneaung/to-raw-sql", 3 | "description": "Get raw SQL from Laravel Query Builder", 4 | "keywords": [ 5 | "pyaesoneaung", 6 | "laravel", 7 | "to-raw-sql" 8 | ], 9 | "homepage": "https://github.com/PyaeSoneAungRgn/to-raw-sql", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Pyae Sone Aung", 14 | "email": "pyaesoneaungrgn@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.1", 20 | "spatie/laravel-package-tools": "^1.14.0", 21 | "illuminate/contracts": "^9.0|^10.0" 22 | }, 23 | "require-dev": { 24 | "laravel/pint": "^1.0", 25 | "nunomaduro/collision": "^6.0", 26 | "nunomaduro/larastan": "^2.0.1", 27 | "orchestra/testbench": "^7.0|^8.0", 28 | "pestphp/pest": "^1.21", 29 | "pestphp/pest-plugin-laravel": "^1.1", 30 | "phpstan/extension-installer": "^1.1", 31 | "phpstan/phpstan-deprecation-rules": "^1.0", 32 | "phpstan/phpstan-phpunit": "^1.0", 33 | "phpunit/phpunit": "^9.5" 34 | }, 35 | "autoload": { 36 | "psr-4": { 37 | "PyaeSoneAung\\ToRawSql\\": "src" 38 | } 39 | }, 40 | "autoload-dev": { 41 | "psr-4": { 42 | "PyaeSoneAung\\ToRawSql\\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 | "PyaeSoneAung\\ToRawSql\\ToRawSqlServiceProvider" 63 | ] 64 | } 65 | }, 66 | "minimum-stability": "dev", 67 | "prefer-stable": true 68 | } -------------------------------------------------------------------------------- /src/Concerns/EloquentBuilderMacros.php: -------------------------------------------------------------------------------- 1 | toRawSql(); 19 | }); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Concerns/QueryBuilderMacros.php: -------------------------------------------------------------------------------- 1 | toRawSql(); 19 | }); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/Exceptions/ToRawSqlException.php: -------------------------------------------------------------------------------- 1 | builder->toSql(), '?'); 17 | $bindingCount = count($this->builder->getBindings()); 18 | if ($placeholderCount !== $bindingCount) { 19 | throw new ToRawSqlException('The `toRawSql()` method is unable to generate raw SQL for your query at this time. This error may occur if your query contains PostgreSQL jsonb operators. Please use the `toSql()` method instead to retrieve the SQL generated by the query builder.'); 20 | } 21 | } 22 | 23 | public function toRawSql(): string 24 | { 25 | $bindings = $this->builder->getBindings(); 26 | foreach ($bindings as $key => $value) { 27 | if ($value instanceof DateTimeInterface) { 28 | $bindings[$key] = "'{$value->format('Y-m-d H:i:s')}'"; 29 | } elseif (is_string($value)) { 30 | $bindings[$key] = "'{$value}'"; 31 | } elseif (is_bool($value)) { 32 | $bindings[$key] = $value ? 'true' : 'false'; 33 | } 34 | } 35 | 36 | return Str::replaceArray('?', $bindings, $this->builder->toSql()); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/ToRawSqlServiceProvider.php: -------------------------------------------------------------------------------- 1 | registerEloquentBuilderMacros(); 21 | $this->registerQueryBuilderMacros(); 22 | } 23 | } 24 | 25 | public function configurePackage(Package $package): void 26 | { 27 | /* 28 | * This class is a Package Service Provider 29 | * 30 | * More info: https://github.com/spatie/laravel-package-tools 31 | */ 32 | $package 33 | ->name('to-raw-sql'); 34 | } 35 | } 36 | --------------------------------------------------------------------------------