Skip to content

Commit

Permalink
rename method
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 9, 2021
1 parent 68b35ca commit 048ac6d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
40 changes: 20 additions & 20 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ public function chunk($count, callable $callback)
return true;
}

/**
* Run a map over each item while chunking.
*
* @param callable $callback
* @param int $count
* @return \Illuminate\Support\Collection
*/
public function chunkMap(callable $callback, $count = 1000)
{
$collection = Collection::make();

$this->chunk($count, function ($items) use ($collection, $callback) {
$items->each(function ($item) use ($collection, $callback) {
$collection->push($callback($item));
});
});

return $collection;
}

/**
* Execute a callback over each item while chunking.
*
Expand Down Expand Up @@ -150,26 +170,6 @@ public function first($columns = ['*'])
return $this->take(1)->get($columns)->first();
}

/**
* Run a map over each item while chunking.
*
* @param callable $callback
* @param int $count
* @return \Illuminate\Support\Collection
*/
public function map(callable $callback, $count = 1000)
{
$collection = Collection::make();

$this->chunk($count, function ($items) use ($collection, $callback) {
$items->each(function ($item) use ($collection, $callback) {
$collection->push($callback($item));
});
});

return $collection;
}

/**
* Execute the query and get the first result if it's the sole matching record.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Database/EloquentWhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function testSoleFailsIfNoRecords()
$this->assertSame(UserWhereTest::class, $exception->getModel());
}

public function testMap()
public function testChunkMap()
{
UserWhereTest::create([
'name' => 'first-name',
Expand All @@ -152,7 +152,7 @@ public function testMap()

DB::enableQueryLog();

$results = UserWhereTest::orderBy('id')->map(function ($user) {
$results = UserWhereTest::orderBy('id')->chunkMap(function ($user) {
return $user->name;
}, 1);

Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/Database/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,11 @@ public function testPaginateWithSpecificColumns()
]);
}

public function testMap()
public function testChunkMap()
{
DB::enableQueryLog();

$results = DB::table('posts')->orderBy('id')->map(function ($post) {
$results = DB::table('posts')->orderBy('id')->chunkMap(function ($post) {
return $post->title;
}, 1);

Expand Down

0 comments on commit 048ac6d

Please sign in to comment.