@@ -245,36 +245,6 @@ public function updateOrCreate(array $attributes, array $values = [])
245245
246246 return $ instance ;
247247 }
248- /**
249- * Chunk the results of the query.
250- *
251- * @param int $count
252- * @param callable $callback
253- * @return bool
254- */
255- public function chunk ($ count , callable $ callback )
256- {
257- $ builder = $ this ->getBuilder ();
258- return $ builder ->chunk ($ count ,$ callback );
259- }
260-
261- /**
262- * Execute a callback over each item while chunking.
263- *
264- * @param callable $callback
265- * @param int $count
266- * @return bool
267- */
268- public function each (callable $ callback , $ count = 1000 )
269- {
270- return $ this ->chunk ($ count , function ($ results ) use ($ callback ) {
271- foreach ($ results as $ key => $ value ) {
272- if ($ callback ($ value , $ key ) === false ) {
273- return false ;
274- }
275- }
276- });
277- }
278248
279249 /**
280250 * Execute the query and get the first related model.
@@ -384,7 +354,8 @@ public function getResults()
384354 */
385355 public function get ($ columns = ['* ' ])
386356 {
387- $ builder = $ this ->getBuilder ($ columns );
357+ $ builder = $ this ->prepareQueryBuilder ($ columns );
358+
388359 $ models = $ builder ->getModels ();
389360
390361 // If we actually found models we will also eager load any relationships that
@@ -444,6 +415,49 @@ protected function shouldSelect(array $columns = ['*'])
444415 return array_merge ($ columns , [$ this ->getQualifiedFirstKeyName ()]);
445416 }
446417
418+ /**
419+ * Chunk the results of the query.
420+ *
421+ * @param int $count
422+ * @param callable $callback
423+ * @return bool
424+ */
425+ public function chunk ($ count , callable $ callback )
426+ {
427+ return $ this ->prepareQueryBuilder ()->chunk ($ count ,$ callback );
428+ }
429+
430+ /**
431+ * Execute a callback over each item while chunking.
432+ *
433+ * @param callable $callback
434+ * @param int $count
435+ * @return bool
436+ */
437+ public function each (callable $ callback , $ count = 1000 )
438+ {
439+ return $ this ->chunk ($ count , function ($ results ) use ($ callback ) {
440+ foreach ($ results as $ key => $ value ) {
441+ if ($ callback ($ value , $ key ) === false ) {
442+ return false ;
443+ }
444+ }
445+ });
446+ }
447+
448+ /**
449+ * Prepare the query builder for query execution.
450+ *
451+ * @param array $columns
452+ * @return \Illuminate\Database\Eloquent\Builder
453+ */
454+ protected function prepareQueryBuilder ($ columns = ['* ' ])
455+ {
456+ return $ this ->query ->applyScopes ()->addSelect (
457+ $ this ->shouldSelect ($ this ->query ->getQuery ()->columns ? [] : $ columns )
458+ );
459+ }
460+
447461 /**
448462 * Add the constraints for a relationship query.
449463 *
@@ -539,22 +553,4 @@ public function getQualifiedLocalKeyName()
539553 {
540554 return $ this ->farParent ->qualifyColumn ($ this ->localKey );
541555 }
542-
543- /**
544- * Add the proper select columns onto the query so it is run with the proper
545- * columns. and return a builder instance with the correct columns.
546- *
547- * @param array $columns
548- * @return \Illuminate\Database\Eloquent\Builder
549- */
550- private function getBuilder ($ columns = ['* ' ]){
551-
552- $ columns = $ this ->query ->getQuery ()->columns ? [] : $ columns ;
553-
554- $ builder = $ this ->query ->applyScopes ();
555-
556- return $ builder ->addSelect (
557- $ this ->shouldSelect ($ columns )
558- );
559- }
560556}
0 commit comments