@@ -73,7 +73,7 @@ public static function sendError(
7373
7474 protected function getDataByRequest (
7575 Request $ request ,
76- Model $ model ,
76+ Model | Builder $ modelOrBuilder ,
7777 array $ queryColumns ,
7878 string $ initialSortColumn
7979 ): Collection
@@ -86,95 +86,98 @@ protected function getDataByRequest(
8686
8787 $ sep = $ queries ['separator ' ] ?? false ;
8888
89- $ data = $ model ->query ();
89+ // Normalize to Builder
90+ $ query = $ modelOrBuilder instanceof Model
91+ ? $ modelOrBuilder ->newQuery ()
92+ : $ modelOrBuilder ;
9093
9194 foreach ($ queries as $ queryName => $ queryValue ) {
9295 if (array_key_exists ($ queryName , $ queryColumns )) {
9396 if ($ sep ) {
9497 $ queryValueArray = array_map ('trim ' , explode ($ sep , $ queryValue ));
95- $ data ->whereIn ($ queryColumns [$ queryName ], $ queryValueArray );
98+ $ query ->whereIn ($ queryColumns [$ queryName ], $ queryValueArray );
9699 continue ;
97100 }
98101
99102 if ($ broadMatch === false ) {
100- $ data ->where ($ queryColumns [$ queryName ], $ queryValue );
103+ $ query ->where ($ queryColumns [$ queryName ], $ queryValue );
101104 } else {
102- $ data ->where ($ queryColumns [$ queryName ], 'LIKE ' , '% ' . $ queryValue . '% ' );
105+ $ query ->where ($ queryColumns [$ queryName ], 'LIKE ' , '% ' . $ queryValue . '% ' );
103106 }
104107 }
105108 }
106109
107- $ data = $ this ->filterDataByAreaCoordinates ($ data , $ queries );
110+ $ query = $ this ->filterDataByAreaCoordinates ($ query , $ queries );
108111
109- $ data = $ this ->filterDataByDateTime ($ data , $ queries );
112+ $ query = $ this ->filterDataByDateTime ($ query , $ queries );
110113
111- $ data = $ this ->filterDataByQueries ($ data , $ queries , $ initialSortColumn );
114+ $ query = $ this ->filterDataByQueries ($ query , $ queries , $ initialSortColumn );
112115
113- return $ data ;
116+ return $ query -> get () ;
114117 }
115118
116- protected function filterDataByDateTime (Builder $ data , array $ queries ): Builder
119+ protected function filterDataByDateTime (Builder $ query , array $ queries ): Builder
117120 {
118121 $ to = isset ($ queries ['to ' ]) ? Carbon::parse ($ queries ['to ' ]) : null ;
119122 $ from = isset ($ queries ['from ' ]) ? Carbon::parse ($ queries ['from ' ]) : null ;
120123 $ timeColumn = $ queries ['timeColumn ' ] ?? 'Timestamp ' ;
121124
122125 if ($ from && $ to ) {
123- $ data ->whereBetween ($ timeColumn , [$ from , $ to ]);
126+ $ query ->whereBetween ($ timeColumn , [$ from , $ to ]);
124127 } elseif ($ from ) {
125- $ data ->where ($ timeColumn , '>= ' , $ from );
128+ $ query ->where ($ timeColumn , '>= ' , $ from );
126129 } elseif ($ to ) {
127- $ data ->where ($ timeColumn , '<= ' , $ to );
130+ $ query ->where ($ timeColumn , '<= ' , $ to );
128131 }
129132
130- return $ data ;
133+ return $ query ;
131134 }
132135
133- protected function filterDataByAreaCoordinates (Builder $ data , array $ queries ): Builder
136+ protected function filterDataByAreaCoordinates (Builder $ query , array $ queries ): Builder
134137 {
135138 $ areaBounds = ['latMin ' , 'latMax ' , 'lngMin ' , 'lngMax ' ];
136139
137140 $ queries = collect ($ queries );
138141
139142 if (!$ queries ->has ($ areaBounds )) {
140- return $ data ;
143+ return $ query ;
141144 }
142145
143146 if ($ queries ->only ($ areaBounds )->count () !== 4 ) {
144- return $ data ;
147+ return $ query ;
145148 }
146149
147- return $ data ->whereBetween ('Latitude ' , [$ queries ['latMin ' ], $ queries ['latMax ' ]])
150+ return $ query ->whereBetween ('Latitude ' , [$ queries ['latMin ' ], $ queries ['latMax ' ]])
148151 ->whereBetween ('Longitude ' , [$ queries ['lngMin ' ], $ queries ['lngMax ' ]]);
149152 }
150153
151154 protected function filterDataByQueries (
152- Builder $ data ,
155+ Builder $ query ,
153156 array $ queries ,
154157 string $ initialSortColumn
155- ): Collection
158+ ): Builder
156159 {
157160 $ limit = $ queries ['limit ' ] ?? 100 ;
158161 $ page = $ queries ['page ' ] ?? 1 ;
159162 $ orderBy = $ queries ['orderBy ' ] ?? $ initialSortColumn ;
160163 $ orderBy = $ orderBy === 'id ' ? $ initialSortColumn : $ orderBy ;
161164 $ orderDir = $ queries ['orderDir ' ] ?? 'asc ' ;
162165 $ offset = $ limit * ($ page - 1 );
166+ $ count = $ query ->count ();
163167
164168 $ this ->meta = [
165169 'limit ' => (int ) $ limit ,
166170 'currentPage ' => (int ) $ page ,
167- 'lastPage ' => ceil ($ data -> count () / $ limit ),
171+ 'lastPage ' => ceil ($ count / $ limit ),
168172 'fromEntry ' => ($ page - 1 ) * $ limit + 1 ,
169- 'toEntry ' => min ($ page * $ limit , $ data -> count () ),
170- 'totalEntries ' => $ data -> count ()
173+ 'toEntry ' => min ($ page * $ limit , $ count ),
174+ 'totalEntries ' => $ count,
171175 ];
172176
173- $ filtered = $ data
177+ $ filtered = $ query
174178 ->limit ($ limit )
175179 ->offset ($ offset )
176- ->orderBy ($ orderBy , $ orderDir )
177- ->get ();
180+ ->orderBy ($ orderBy , $ orderDir );
178181
179182 return $ filtered ;
180183 }
0 commit comments