Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.0] Eloquent belongsToMany chunk #8045

Merged
merged 2 commits into from
Apr 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ public function paginate($perPage = null, $columns = array('*'))
return $paginator;
}

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

$this->query->chunk($count, function($results) use ($callback)
{
$this->hydratePivotRelation($results->all());

call_user_func($callback, $results);
});
}

/**
* Hydrate the pivot table relationship on the models.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,20 @@ public function testHasOnSelfReferencingBelongsToManyRelationship()
}


public function testBelongsToManyRelationshipModelsAreProperlyHydratedOverChunkedRequest()
{
$user = EloquentTestUser::create(['email' => '[email protected]']);
$friend = $user->friends()->create(['email' => '[email protected]']);

EloquentTestUser::first()->friends()->chunk(2, function($friends) use ($user, $friend){
$this->assertEquals(1, count($friends));
$this->assertEquals('[email protected]', $friends->first()->email);
$this->assertEquals($user->id, $friends->first()->pivot->user_id);
$this->assertEquals($friend->id, $friends->first()->pivot->friend_id);
});
}


public function testBasicHasManyEagerLoading()
{
$user = EloquentTestUser::create(['email' => '[email protected]']);
Expand Down