Skip to content

Commit

Permalink
Add query builder map method
Browse files Browse the repository at this point in the history
  • Loading branch information
reinink authored and taylorotwell committed Feb 9, 2021
1 parent 3d5c097 commit 68b35ca
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Illuminate/Database/Concerns/BuildsQueries.php
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;

trait BuildsQueries
{
Expand Down Expand Up @@ -149,6 +150,26 @@ 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
27 changes: 27 additions & 0 deletions tests/Integration/Database/EloquentWhereTest.php
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\MultipleRecordsFoundException;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

/**
Expand Down Expand Up @@ -134,6 +135,32 @@ public function testSoleFailsIfNoRecords()

$this->assertSame(UserWhereTest::class, $exception->getModel());
}

public function testMap()
{
UserWhereTest::create([
'name' => 'first-name',
'email' => 'first-email',
'address' => 'first-address',
]);

UserWhereTest::create([
'name' => 'second-name',
'email' => 'second-email',
'address' => 'second-address',
]);

DB::enableQueryLog();

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

$this->assertCount(2, $results);
$this->assertSame('first-name', $results[0]);
$this->assertSame('second-name', $results[1]);
$this->assertCount(3, DB::getQueryLog());
}
}

class UserWhereTest extends Model
Expand Down
14 changes: 14 additions & 0 deletions tests/Integration/Database/QueryBuilderTest.php
Expand Up @@ -213,4 +213,18 @@ public function testPaginateWithSpecificColumns()
(object) ['title' => 'Bar Post', 'content' => 'Lorem Ipsum.'],
]);
}

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

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

$this->assertCount(2, $results);
$this->assertSame('Foo Post', $results[0]);
$this->assertSame('Bar Post', $results[1]);
$this->assertCount(3, DB::getQueryLog());
}
}

0 comments on commit 68b35ca

Please sign in to comment.