@@ -107,30 +107,56 @@ protected function useTableAliasInConditions(): self
107107 }
108108
109109 $ this ->wheres = collect ($ this ->wheres )->filter (function ($ where ) {
110- return in_array ($ where ['type ' ] ?? '' , ['Column ' ]);
110+ return in_array ($ where ['type ' ] ?? '' , ['Column ' , ' Basic ' ]);
111111 })->map (function ($ where ) {
112- // dd($where);
113112 $ key = $ this ->model ->getKeyName ();
114113 $ table = $ this ->tableName ;
114+ $ replaceMethod = sprintf ('useAliasInWhere%sType ' , ucfirst ($ where ['type ' ]));
115115
116- // if it was already replaced, skip
117- if (Str::startsWith ($ where ['first ' ] . '. ' , $ this ->alias . '. ' ) || Str::startsWith ($ where ['second ' ] . '. ' , $ this ->alias . '. ' )) {
118- return $ where ;
119- }
116+ return $ this ->{$ replaceMethod }($ where );
117+ })->toArray ();
120118
121- if (Str::contains ($ where ['first ' ], $ table ) && Str::contains ($ where ['second ' ], $ table )) {
122- // if joining the same table, only replace the correct table.key pair
123- $ where ['first ' ] = str_replace ($ table . '. ' . $ key , $ this ->alias . '. ' . $ key , $ where ['first ' ]);
124- $ where ['second ' ] = str_replace ($ table . '. ' . $ key , $ this ->alias . '. ' . $ key , $ where ['second ' ]);
125- } else {
126- $ where ['first ' ] = str_replace ($ table . '. ' , $ this ->alias . '. ' , $ where ['first ' ]);
127- $ where ['second ' ] = str_replace ($ table . '. ' , $ this ->alias . '. ' , $ where ['second ' ]);
128- }
119+ return $ this ;
120+ }
129121
122+ protected function useAliasInWhereColumnType (array $ where ): array
123+ {
124+ $ key = $ this ->model ->getKeyName ();
125+ $ table = $ this ->tableName ;
126+
127+ // if it was already replaced, skip
128+ if (Str::startsWith ($ where ['first ' ] . '. ' , $ this ->alias . '. ' ) || Str::startsWith ($ where ['second ' ] . '. ' , $ this ->alias . '. ' )) {
130129 return $ where ;
131- })-> toArray ();
130+ }
132131
133- return $ this ;
132+ if (Str::contains ($ where ['first ' ], $ table ) && Str::contains ($ where ['second ' ], $ table )) {
133+ // if joining the same table, only replace the correct table.key pair
134+ $ where ['first ' ] = str_replace ($ table . '. ' . $ key , $ this ->alias . '. ' . $ key , $ where ['first ' ]);
135+ $ where ['second ' ] = str_replace ($ table . '. ' . $ key , $ this ->alias . '. ' . $ key , $ where ['second ' ]);
136+ } else {
137+ $ where ['first ' ] = str_replace ($ table . '. ' , $ this ->alias . '. ' , $ where ['first ' ]);
138+ $ where ['second ' ] = str_replace ($ table . '. ' , $ this ->alias . '. ' , $ where ['second ' ]);
139+ }
140+
141+ return $ where ;
142+ }
143+
144+ protected function useAliasInWhereBasicType (array $ where ): array
145+ {
146+ $ table = $ this ->tableName ;
147+
148+ if (Str::startsWith ($ where ['column ' ] . '. ' , $ this ->alias . '. ' )) {
149+ return $ where ;
150+ }
151+
152+ if (Str::contains ($ where ['column ' ], $ table )) {
153+ // if joining the same table, only replace the correct table.key pair
154+ $ where ['column ' ] = str_replace ($ table . '. ' , $ this ->alias . '. ' , $ where ['column ' ]);
155+ } else {
156+ $ where ['column ' ] = str_replace ($ table . '. ' , $ this ->alias . '. ' , $ where ['column ' ]);
157+ }
158+
159+ return $ where ;
134160 }
135161
136162 public function whereNull ($ columns , $ boolean = 'and ' , $ not = false )
0 commit comments