Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 3, 2018
1 parent 5739f74 commit 5d3d98a
Showing 1 changed file with 45 additions and 49 deletions.
94 changes: 45 additions & 49 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,36 +245,6 @@ public function updateOrCreate(array $attributes, array $values = [])

return $instance;
}
/**
* Chunk the results of the query.
*
* @param int $count
* @param callable $callback
* @return bool
*/
public function chunk($count, callable $callback)
{
$builder = $this->getBuilder();
return $builder->chunk($count,$callback);
}

/**
* 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 related model.
Expand Down Expand Up @@ -384,7 +354,8 @@ public function getResults()
*/
public function get($columns = ['*'])
{
$builder = $this->getBuilder($columns);
$builder = $this->prepareQueryBuilder($columns);

$models = $builder->getModels();

// If we actually found models we will also eager load any relationships that
Expand Down Expand Up @@ -444,6 +415,49 @@ protected function shouldSelect(array $columns = ['*'])
return array_merge($columns, [$this->getQualifiedFirstKeyName()]);
}

/**
* Chunk the results of the query.
*
* @param int $count
* @param callable $callback
* @return bool
*/
public function chunk($count, callable $callback)
{
return $this->prepareQueryBuilder()->chunk($count,$callback);
}

/**
* 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;
}
}
});
}

/**
* Prepare the query builder for query execution.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function prepareQueryBuilder($columns = ['*'])
{
return $this->query->applyScopes()->addSelect(
$this->shouldSelect($this->query->getQuery()->columns ? [] : $columns)
);
}

/**
* Add the constraints for a relationship query.
*
Expand Down Expand Up @@ -539,22 +553,4 @@ public function getQualifiedLocalKeyName()
{
return $this->farParent->qualifyColumn($this->localKey);
}

/**
* Add the proper select columns onto the query so it is run with the proper
* columns. and return a builder instance with the correct columns.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Builder
*/
private function getBuilder($columns = ['*']){

$columns = $this->query->getQuery()->columns ? [] : $columns;

$builder = $this->query->applyScopes();

return $builder->addSelect(
$this->shouldSelect($columns)
);
}
}

0 comments on commit 5d3d98a

Please sign in to comment.