7
7
8
8
## Package description
9
9
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
+ ```
11
21
12
22
## Requirement
13
23
@@ -34,22 +44,19 @@ After installing this package you can use these methods on eloquent and database
34
44
35
45
- ` getSql `
36
46
* 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 .
39
49
40
50
- ` dumpSql `
41
51
* This method prints sql query (uses dump() function)
42
52
* Returns builder.
43
53
44
54
- ` logSql `
45
55
* This method logs sql query.
56
+ * Optionally takes prefix string. Which prepends to sql string.
46
57
* Log type can be set in config file (default is "info").
47
58
* Returns builder.
48
59
49
- - ` getSqlFunc `
50
- * This method takes closure and gives sql string as parameter.
51
- * Returns builder.
52
-
53
60
## Examples
54
61
55
62
#### Eloquent
@@ -68,14 +75,12 @@ User::select('name')
68
75
->dumpSql()
69
76
->where('surname', '!=', 'doe')
70
77
->where('email', 'LIKE', '%example%')
71
- ->getSqlFunc (function(string $sql) {
78
+ ->getSql (function(string $sql) {
72
79
echo $sql;
73
- })
74
- ->get();
80
+ });
75
81
// select `name` from `users` where `id` = 5
76
82
// select `name` from `users` where `id` = 5 and `name` != 'john'
77
83
// select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%'
78
- // []
79
84
```
80
85
81
86
#### Database Builder
@@ -92,14 +97,12 @@ User::select('name')
92
97
->dumpSql()
93
98
->where('surname', '!=', 'doe')
94
99
->where('email', 'LIKE', '%example%')
95
- ->getSqlFunc (function(string $sql) {
100
+ ->getSql (function(string $sql) {
96
101
echo $sql;
97
- })
98
- ->get();
102
+ });
99
103
// select `name` from `users` where `id` = 5
100
104
// select `name` from `users` where `id` = 5 and `name` != 'john'
101
105
// select `name` from `users` where `id` = 5 and `name` != 'john' and `surname` != 'doe' and `email` LIKE '%example%'
102
- // []
103
106
```
104
107
105
108
#### Replace bindings for all queries
0 commit comments