77
88## Package description
99
10- This package adds methods for getting sql query to eloquent and database query builder.
10+ This package adds custom sql methods to eloquent and database query builder.
11+
12+ Laravel's ` toSql() ` method gives you sql query without bindings replaced.
13+ ``` sql
14+ select ` name` from ` users` where ` id` = ? and ` name` = ?
15+ ```
16+
17+ With this package you have ` getSql() ` method. Which gives you same result with bindings replaced.
18+ ``` sql
19+ select ` name` from ` users` where ` id` = 5 and ` name` = ' laravel'
20+ ```
1121
1222## Requirement
1323
@@ -34,22 +44,19 @@ After installing this package you can use these methods on eloquent and database
3444
3545- ` getSql `
3646 * This method returns sql query.
37- * Differences between ` toSql ` and this method is this method returns sql with question marks (?) replaced .
38- * Returns string.
47+ * Optionally takes closure and calls closure with sql parameter .
48+ * Returns string or closure result .
3949
4050- ` dumpSql `
4151 * This method prints sql query (uses dump() function)
4252 * Returns builder.
4353
4454- ` logSql `
4555 * This method logs sql query.
56+ * Optionally takes prefix string. Which prepends to sql string.
4657 * Log type can be set in config file (default is "info").
4758 * Returns builder.
4859
49- - ` getSqlFunc `
50- * This method takes closure and gives sql string as parameter.
51- * Returns builder.
52-
5360## Examples
5461
5562#### Eloquent
@@ -68,14 +75,12 @@ User::select('name')
6875 ->dumpSql()
6976 ->where('surname', '!=', 'doe')
7077 ->where('email', 'LIKE', '%example%')
71- ->getSqlFunc (function(string $sql) {
78+ ->getSql (function(string $sql) {
7279 echo $sql;
73- })
74- ->get();
80+ });
7581// select `name` from `users` where `id` = 5
7682// select `name` from `users` where `id` = 5 and `name` != 'john'
7783// select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%'
78- // []
7984```
8085
8186#### Database Builder
@@ -92,14 +97,12 @@ User::select('name')
9297 ->dumpSql()
9398 ->where('surname', '!=', 'doe')
9499 ->where('email', 'LIKE', '%example%')
95- ->getSqlFunc (function(string $sql) {
100+ ->getSql (function(string $sql) {
96101 echo $sql;
97- })
98- ->get();
102+ });
99103// select `name` from `users` where `id` = 5
100104// select `name` from `users` where `id` = 5 and `name` != 'john'
101105// select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%'
102- // []
103106```
104107
105108#### Replace bindings for all queries
0 commit comments