22
33namespace Weebly \Mutate \Database ;
44
5- use Closure ;
65use Illuminate \Support \Str ;
76use Illuminate \Database \Eloquent \Builder ;
7+ use Illuminate \Database \Query \Expression ;
88
99class EloquentBuilder extends Builder
1010{
@@ -18,34 +18,87 @@ class EloquentBuilder extends Builder
1818 */
1919 public function where ($ column , $ operator = null , $ value = null , $ boolean = 'and ' )
2020 {
21- if ($ column instanceof Closure) {
22- return parent ::where ($ column , $ operator , $ value , $ boolean );
23- }
21+ $ bindings = $ this ->query ->bindings ;
22+
23+ parent ::where ($ column , $ operator , $ value , $ boolean );
24+
25+ // Remove the last item of the array
26+ $ where = array_pop ($ this ->query ->wheres );
2427
25- if (func_num_args () === 2 ) {
26- $ value = $ operator ;
27- $ operator = '= ' ;
28+ if ($ where ['type ' ] === 'Nested ' ) {
29+ // Add where statement back and return
30+ $ this ->query ->wheres [] = $ where ;
31+
32+ return $ this ;
2833 }
2934
30- $ mutatedColumn = $ this ->getUnqualifiedColumnName ($ column );
31- $ value = $ this ->model ->serializeAttribute ($ mutatedColumn , $ value );
35+ // Get the column name
36+ $ mutatedColumn = $ this ->getUnqualifiedColumnName ($ where ['column ' ]);
37+
38+ // Modify the values
39+ $ where ['value ' ] = $ this ->model ->serializeAttribute ($ mutatedColumn , $ where ['value ' ]);
3240
33- return parent ::where ($ mutatedColumn , $ operator , $ value , $ boolean );
41+ // Add where statement back
42+ $ this ->query ->wheres [] = $ where ;
43+
44+ // Add the mutated bindings
45+ if (! $ where ['value ' ] instanceof Expression) {
46+ // Reset the bindings to the previous value
47+ $ this ->query ->bindings = $ bindings ;
48+
49+ // Add the mutated bindings back
50+ $ this ->addBinding ($ where ['value ' ], 'where ' );
51+ }
52+
53+ return $ this ;
3454 }
3555
3656 /**
3757 * {@inheritdoc}
3858 */
3959 public function whereIn ($ column , $ values , $ boolean = 'and ' , $ not = false )
4060 {
41- $ mutatedColumn = $ this ->getUnqualifiedColumnName ($ column );
61+ $ bindings = $ this ->query ->bindings ;
62+
63+ parent ::whereIn ($ column , $ values , $ boolean , $ not );
4264
65+ // Remove the last item of the array
66+ $ where = array_pop ($ this ->query ->wheres );
67+
68+ if ($ where ['type ' ] === 'Nested ' ) {
69+ // Add where statement back and return
70+ $ this ->query ->wheres [] = $ where ;
71+
72+ return $ this ;
73+ }
74+
75+ // Get the column name
76+ $ mutatedColumn = $ this ->getUnqualifiedColumnName ($ where ['column ' ]);
77+
78+ // Loop over all values and mutate them
4379 $ mutatedValues = [];
44- foreach ($ values as $ value ) {
80+ foreach ($ where [ ' values ' ] as $ value ) {
4581 $ mutatedValues [] = $ this ->model ->serializeAttribute ($ mutatedColumn , $ value );
4682 }
4783
48- return parent ::whereIn ($ column , $ mutatedValues , $ boolean , $ not );
84+ // Modify the values
85+ $ where ['values ' ] = $ mutatedValues ;
86+
87+ // Add where statement back
88+ $ this ->query ->wheres [] = $ where ;
89+
90+ // Loop over all the mutated values and add the bindings
91+ foreach ($ mutatedValues as $ value ) {
92+ if (! $ value instanceof Expression) {
93+ // Reset the bindings to the previous value
94+ $ this ->query ->bindings = $ bindings ;
95+
96+ // Add the mutated bindings back
97+ $ this ->addBinding ($ value , 'where ' );
98+ }
99+ }
100+
101+ return $ this ;
49102 }
50103
51104 /**
0 commit comments