Skip to content

Commit 9f03dc6

Browse files
[12.x] use limit() in place of take() (#56080)
* use `limit()` in place of `take()` in all of these changes, `take()` is a direct alias of the `limit()` method, and performs no additional logic. switching to directly calling the `limit()` method means less code execution (minimallly better performance) and reduces the call stack (nice for debugging). a bit more subjective, but I would also argue that "limit" is a little clearer semantically about what it is doing to the query, whereas "take" has the implication that you're selecting everything and then taking a subset of that result. * fix test mock --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 628d1bb commit 9f03dc6

File tree

10 files changed

+16
-14
lines changed

10 files changed

+16
-14
lines changed

src/Illuminate/Bus/DatabaseBatchRepository.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function get($limit = 50, $before = null)
5959
{
6060
return $this->connection->table($this->table)
6161
->orderByDesc('id')
62-
->take($limit)
62+
->limit($limit)
6363
->when($before, fn ($q) => $q->where('id', '<', $before))
6464
->get()
6565
->map(function ($batch) {
@@ -247,7 +247,7 @@ public function prune(DateTimeInterface $before)
247247
$totalDeleted = 0;
248248

249249
do {
250-
$deleted = $query->take(1000)->delete();
250+
$deleted = $query->limit(1000)->delete();
251251

252252
$totalDeleted += $deleted;
253253
} while ($deleted !== 0);
@@ -270,7 +270,7 @@ public function pruneUnfinished(DateTimeInterface $before)
270270
$totalDeleted = 0;
271271

272272
do {
273-
$deleted = $query->take(1000)->delete();
273+
$deleted = $query->limit(1000)->delete();
274274

275275
$totalDeleted += $deleted;
276276
} while ($deleted !== 0);
@@ -293,7 +293,7 @@ public function pruneCancelled(DateTimeInterface $before)
293293
$totalDeleted = 0;
294294

295295
do {
296-
$deleted = $query->take(1000)->delete();
296+
$deleted = $query->limit(1000)->delete();
297297

298298
$totalDeleted += $deleted;
299299
} while ($deleted !== 0);

src/Illuminate/Database/Concerns/BuildsQueries.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ protected function orderedLazyById($chunkSize = 1000, $column = null, $alias = n
363363
*/
364364
public function first($columns = ['*'])
365365
{
366-
return $this->take(1)->get($columns)->first();
366+
return $this->limit(1)->get($columns)->first();
367367
}
368368

369369
/**
@@ -395,7 +395,7 @@ public function firstOrFail($columns = ['*'], $message = null)
395395
*/
396396
public function sole($columns = ['*'])
397397
{
398-
$result = $this->take(2)->get($columns);
398+
$result = $this->limit(2)->get($columns);
399399

400400
$count = $result->count();
401401

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'p
11191119
// Next we will set the limit and offset for this query so that when we get the
11201120
// results we get the proper section of results. Then, we'll create the full
11211121
// paginator instances for these results with the given page and per page.
1122-
$this->offset(($page - 1) * $perPage)->take($perPage + 1);
1122+
$this->offset(($page - 1) * $perPage)->limit($perPage + 1);
11231123

11241124
return $this->simplePaginator($this->get($columns), $perPage, $page, [
11251125
'path' => Paginator::resolveCurrentPath(),

src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ public function firstWhere($column, $operator = null, $value = null, $boolean =
841841
*/
842842
public function first($columns = ['*'])
843843
{
844-
$results = $this->take(1)->get($columns);
844+
$results = $this->limit(1)->get($columns);
845845

846846
return count($results) > 0 ? $results->first() : null;
847847
}

src/Illuminate/Database/Eloquent/Relations/HasOneOrManyThrough.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public function firstWhere($column, $operator = null, $value = null, $boolean =
279279
*/
280280
public function first($columns = ['*'])
281281
{
282-
$results = $this->take(1)->get($columns);
282+
$results = $this->limit(1)->get($columns);
283283

284284
return count($results) > 0 ? $results->first() : null;
285285
}

src/Illuminate/Database/Eloquent/Relations/Relation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function getEager()
186186
*/
187187
public function sole($columns = ['*'])
188188
{
189-
$result = $this->take(2)->get($columns);
189+
$result = $this->limit(2)->get($columns);
190190

191191
$count = $result->count();
192192

src/Illuminate/Database/Migrations/DatabaseMigrationRepository.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public function getMigrations($steps)
6464

6565
return $query->orderBy('batch', 'desc')
6666
->orderBy('migration', 'desc')
67-
->take($steps)->get()->all();
67+
->limit($steps)
68+
->get()
69+
->all();
6870
}
6971

7072
/**

src/Illuminate/Queue/Failed/DatabaseFailedJobProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function prune(DateTimeInterface $before)
136136
$totalDeleted = 0;
137137

138138
do {
139-
$deleted = $query->take(1000)->delete();
139+
$deleted = $query->limit(1000)->delete();
140140

141141
$totalDeleted += $deleted;
142142
} while ($deleted !== 0);

src/Illuminate/Queue/Failed/DatabaseUuidFailedJobProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public function prune(DateTimeInterface $before)
149149
$totalDeleted = 0;
150150

151151
do {
152-
$deleted = $query->take(1000)->delete();
152+
$deleted = $query->limit(1000)->delete();
153153

154154
$totalDeleted += $deleted;
155155
} while ($deleted !== 0);

tests/Database/DatabaseEloquentBuilderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ public function testFindWithManyUsingCollection()
288288
public function testFirstMethod()
289289
{
290290
$builder = m::mock(Builder::class.'[get,take]', [$this->getMockQueryBuilder()]);
291-
$builder->shouldReceive('take')->with(1)->andReturnSelf();
291+
$builder->shouldReceive('limit')->with(1)->andReturnSelf();
292292
$builder->shouldReceive('get')->with(['*'])->andReturn(new Collection(['bar']));
293293

294294
$result = $builder->first();

0 commit comments

Comments
 (0)