Skip to content

Commit e807c0a

Browse files
add queryToSql row query helper method
1 parent 8f55c0c commit e807c0a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

docs/helper.md

+9
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,13 @@ This method returns the application namespace.
1414
appNamespace(); // return 'App\\'
1515
```
1616

17+
### queryToSql($query)
18+
19+
This method returns the SQL query of a query builder.
20+
21+
```php
22+
$query = DB::table('users')->where('votes', '>', 100);
23+
queryToSql($query); // return 'select * from `users` where `votes` > 100'
24+
```
25+
1726
- [Next To Boolean Status](https://github.com/jobmetric/laravel-package-core/blob/master/docs/boolean-status.md)

src/helpers.php

+17-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
use Illuminate\Container\Container;
44
use Illuminate\Contracts\Foundation\Application;
5+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6+
use Illuminate\Database\Query\Builder as QueryBuilder;
57

6-
if(!function_exists('appNamespace')) {
8+
if (!function_exists('appNamespace')) {
79
/**
810
* Get the application namespace for the application.
911
*
@@ -20,3 +22,17 @@ function appNamespace(): string
2022
}
2123
}
2224
}
25+
26+
if (!function_exists('queryToSql')) {
27+
/**
28+
* get full sql query string in query builder
29+
*
30+
* @param EloquentBuilder|QueryBuilder $builder
31+
*
32+
* @return string
33+
*/
34+
function queryToSql(EloquentBuilder|QueryBuilder $builder): string
35+
{
36+
return vsprintf(str_replace('?', '%s', str_replace('?', "'?'", $builder->toSql())), $builder->getBindings());
37+
}
38+
}

0 commit comments

Comments
 (0)