@@ -176,23 +176,21 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
176
176
}
177
177
178
178
// This is an "orWhere" type query, so add a find request and then work from there
179
- if ($ boolean === 'or ' || $ shouldBeOmit ) {
179
+ if ($ boolean === 'or ' || ( $ shouldBeOmit && ! $ this -> isCurrentFindAnOmit ()) ) {
180
180
$ this ->addFindRequest ();
181
+ }
181
182
182
183
if ($ shouldBeOmit ) {
183
184
$ this ->omit ();
184
185
}
185
- }
186
186
187
187
// If the column is an array, we will assume it is an array of key-value pairs
188
188
// and can add them each as a where clause. We will maintain the boolean we
189
189
// received when the method was called and pass it into the nested where.
190
190
//
191
191
// If the first value is an array it means the second value is an omit for the whole request
192
192
if (is_array ($ column )) {
193
- foreach ($ column as $ eachColumn => $ eachValue ) {
194
- $ this ->where ($ eachColumn , $ eachValue );
195
- }
193
+ $ this ->addArrayOfWheres ($ column , $ boolean );
196
194
197
195
return $ this ;
198
196
}
@@ -214,6 +212,19 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
214
212
return $ this ;
215
213
}
216
214
215
+ protected function addArrayOfWheres ($ column , $ boolean , $ method = 'where ' )
216
+ {
217
+ foreach ($ column as $ key => $ value ) {
218
+ if (is_numeric ($ key ) && is_array ($ value )) {
219
+ $ this ->{$ method }(...array_values ($ value ));
220
+ } else {
221
+ $ this ->{$ method }($ key , '' , $ value , 'and ' );
222
+ }
223
+ }
224
+
225
+ return $ this ;
226
+ }
227
+
217
228
/**
218
229
* Delete records from the database.
219
230
*
@@ -572,6 +583,15 @@ protected function getCurrentFind()
572
583
return $ this ->wheres [$ this ->currentFindRequestIndex ];
573
584
}
574
585
586
+ protected function isCurrentFindAnOmit ()
587
+ {
588
+ if ($ this ->currentFindRequestIndex === -1 ) {
589
+ return false ;
590
+ }
591
+
592
+ return Arr::get ($ this ->wheres , "{$ this ->currentFindRequestIndex }.omit " , 'false ' ) === 'true ' ;
593
+ }
594
+
575
595
protected function updateCurrentFind ($ find )
576
596
{
577
597
$ this ->wheres [$ this ->currentFindRequestIndex ] = $ find ;
@@ -606,11 +626,11 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
606
626
return $ this ;
607
627
}
608
628
609
- protected function computeWhereIns ()
629
+ public function computeWhereIns ()
610
630
{
611
631
// If no where in clauses return
612
632
if (empty ($ this ->whereIns )) {
613
- return ;
633
+ return $ this ;
614
634
}
615
635
616
636
$ whereInRequests = collect ($ this ->whereIns )->mapToGroups (function ($ whereIn ) {
@@ -643,37 +663,57 @@ protected function computeWhereIns()
643
663
});
644
664
645
665
if ($ this ->isWheresEmpty ()) {
646
- $ this ->wheres = $ whereInRequests ->flatten (2 )->toArray ();
666
+ $ this ->wheres = $ whereInRequests ->map (function ($ whereInRequest ) {
667
+ return Arr::crossJoin (...$ whereInRequest ->values ());
668
+ })->map (function ($ findRequest ) {
669
+ return array_map (function ($ clauses ) {
670
+ return array_merge (...$ clauses );
671
+ }, $ findRequest );
672
+ })->flatten (1 )->toArray ();
647
673
648
- return ;
674
+ return $ this ;
649
675
}
650
676
651
- $ newWheres = [] ;
677
+ $ newWheres = collect ([]) ;
652
678
653
679
// loop through each where
654
680
// If it is an omit, skip it
655
681
// If the where in is an omit, skip it
656
682
foreach ($ this ->wheres as $ index => $ where ) {
657
- $ whereInValues = $ whereInRequests ->get ($ index)?->first( ) ?? [];
683
+ $ whereInRequest = $ whereInRequests ->get ($ index ) ?? [];
658
684
659
685
if (($ where ['omit ' ] ?? 'false ' ) === 'true ' ) {
660
- if (count (array_keys ($ where )) > 1 || (count (array_keys ($ where )) === 1 && (collect ($ whereInValues )->value ('omit ' ) ?? 'false ' ) === 'false ' )) {
661
- $ newWheres[] = $ where ;
686
+ if (count (array_keys ($ where )) > 1 || (count (array_keys ($ where )) === 1 && (collect ($ whereInRequest )->value ('omit ' ) ?? 'false ' ) === 'false ' )) {
687
+ $ newWheres-> push ( $ where) ;
662
688
663
689
continue ;
664
690
}
665
691
}
666
692
667
- if (empty ($ whereInValues )) {
668
- $ newWheres[] = $ where ;
693
+ if (empty ($ whereInRequest )) {
694
+ $ newWheres-> push ( $ where) ;
669
695
} else {
670
- foreach ($ whereInValues as $ whereInValue ) {
671
- $ newWheres [] = array_merge ($ where , $ whereInValue );
672
- }
696
+ $ newWheres = $ newWheres ->push (...Arr::crossJoin ([$ where ], ...$ whereInRequest ->values ()));
673
697
}
674
698
}
675
699
676
- $ this ->wheres = $ newWheres ;
700
+ $ this ->wheres = $ newWheres
701
+ ->map (function ($ findRequest ) {
702
+ if (Arr::isAssoc ($ findRequest )) {
703
+ return $ findRequest ;
704
+ }
705
+
706
+ return array_merge (...$ findRequest );
707
+ })->toArray ();
708
+
709
+ return $ this ;
710
+ }
711
+
712
+ public function getWheres ()
713
+ {
714
+ $ this ->computeWhereIns ();
715
+
716
+ return $ this ->wheres ;
677
717
}
678
718
679
719
/**
0 commit comments