forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
536 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Concerns; | ||
|
||
trait BuildsQueries | ||
{ | ||
/** | ||
* Chunk the results of the query. | ||
* | ||
* @param int $count | ||
* @param callable $callback | ||
* @return bool | ||
*/ | ||
public function chunk($count, callable $callback) | ||
{ | ||
$this->enforceOrderBy(); | ||
|
||
$page = 1; | ||
|
||
do { | ||
// We'll execute the query for the given page and get the results. If there are | ||
// no results we can just break and return from here. When there are results | ||
// we will call the callback with the current chunk of these results here. | ||
$results = $this->forPage($page, $count)->get(); | ||
|
||
$countResults = $results->count(); | ||
|
||
if ($countResults == 0) { | ||
break; | ||
} | ||
|
||
// On each chunk result set, we will pass them to the callback and then let the | ||
// developer take care of everything within the callback, which allows us to | ||
// keep the memory low for spinning through large result sets for working. | ||
if ($callback($results) === false) { | ||
return false; | ||
} | ||
|
||
$page++; | ||
} while ($countResults == $count); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Execute a callback over each item while chunking. | ||
* | ||
* @param callable $callback | ||
* @param int $count | ||
* @return bool | ||
*/ | ||
public function each(callable $callback, $count = 1000) | ||
{ | ||
return $this->chunk($count, function ($results) use ($callback) { | ||
foreach ($results as $key => $value) { | ||
if ($callback($value, $key) === false) { | ||
return false; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Execute the query and get the first result. | ||
* | ||
* @param array $columns | ||
* @return mixed | ||
*/ | ||
public function first($columns = ['*']) | ||
{ | ||
return $this->take(1)->get($columns)->first(); | ||
} | ||
|
||
/** | ||
* Apply the callback's query changes if the given "value" is true. | ||
* | ||
* @param mixed $value | ||
* @param \Closure $callback | ||
* @param \Closure $default | ||
* @return mixed | ||
*/ | ||
public function when($value, $callback, $default = null) | ||
{ | ||
if ($value) { | ||
return $callback($this, $value) ?: $this; | ||
} elseif ($default) { | ||
return $default($this, $value) ?: $this; | ||
} | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.