@@ -1039,10 +1039,7 @@ public function grabNumRecords($table, $attributes = [])
10391039 */
10401040 protected function findModel ($ modelClass , $ attributes = [])
10411041 {
1042- $ query = $ this ->getQueryBuilderFromModel ($ modelClass );
1043- foreach ($ attributes as $ key => $ value ) {
1044- $ query ->where ($ key , $ value );
1045- }
1042+ $ query = $ this ->buildQuery ($ modelClass , $ attributes );
10461043
10471044 return $ query ->first ();
10481045 }
@@ -1054,11 +1051,7 @@ protected function findModel($modelClass, $attributes = [])
10541051 */
10551052 protected function findRecord ($ table , $ attributes = [])
10561053 {
1057- $ query = $ this ->getQueryBuilderFromTable ($ table );
1058- foreach ($ attributes as $ key => $ value ) {
1059- $ query ->where ($ key , $ value );
1060- }
1061-
1054+ $ query = $ this ->buildQuery ($ table , $ attributes );
10621055 return (array ) $ query ->first ();
10631056 }
10641057
@@ -1069,11 +1062,7 @@ protected function findRecord($table, $attributes = [])
10691062 */
10701063 protected function countModels ($ modelClass , $ attributes = [])
10711064 {
1072- $ query = $ this ->getQueryBuilderFromModel ($ modelClass );
1073- foreach ($ attributes as $ key => $ value ) {
1074- $ query ->where ($ key , $ value );
1075- }
1076-
1065+ $ query = $ this ->buildQuery ($ modelClass , $ attributes );
10771066 return $ query ->count ();
10781067 }
10791068
@@ -1084,11 +1073,7 @@ protected function countModels($modelClass, $attributes = [])
10841073 */
10851074 protected function countRecords ($ table , $ attributes = [])
10861075 {
1087- $ query = $ this ->getQueryBuilderFromTable ($ table );
1088- foreach ($ attributes as $ key => $ value ) {
1089- $ query ->where ($ key , $ value );
1090- }
1091-
1076+ $ query = $ this ->buildQuery ($ table , $ attributes );
10921077 return $ query ->count ();
10931078 }
10941079
@@ -1243,4 +1228,32 @@ private function getDomainRegex($route)
12431228
12441229 return $ compiledRoute ->getHostRegex ();
12451230 }
1231+
1232+ /**
1233+ * Build Eloquent query with attributes
1234+ *
1235+ * @param string $table
1236+ * @param array $attributes
1237+ * @return EloquentModel
1238+ * @part orm
1239+ */
1240+ private function buildQuery ($ table , $ attributes = [])
1241+ {
1242+ if (class_exists ($ table )) {
1243+ $ query = $ this ->getQueryBuilderFromModel ($ table );
1244+ } else {
1245+ $ query = $ this ->getQueryBuilderFromTable ($ table );
1246+ }
1247+
1248+ foreach ($ attributes as $ key => $ value ) {
1249+ if (\is_array ($ value )) {
1250+ call_user_func_array (array ($ query , "where " ), $ value );
1251+ } elseif (is_null ($ value )) {
1252+ $ query ->whereNull ($ key );
1253+ } else {
1254+ $ query ->where ($ key , $ value );
1255+ }
1256+ }
1257+ return $ query ;
1258+ }
12461259}
0 commit comments