Skip to content

Commit

Permalink
[5.x] Make limit modifier work with query builders (#10818)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
aerni and jasonvarga authored Sep 18, 2024
1 parent 31bcd47 commit 1caf2ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,10 @@ public function limit($value, $params)
{
$limit = Arr::get($params, 0, 0);

if (Compare::isQueryBuilder($value)) {
return $value->limit($limit);
}

if ($value instanceof Collection) {
return $value->take($limit);
}
Expand Down
15 changes: 13 additions & 2 deletions tests/Modifiers/LimitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Contracts\Query\Builder;
use Statamic\Modifiers\Modify;
use Tests\TestCase;

Expand Down Expand Up @@ -34,8 +35,18 @@ public function it_limits_collections(): void
$this->assertEquals(['one', 'two', 'three'], $limited->all());
}

public function modify($arr, $limit)
#[Test]
public function it_limits_builders(): void
{
$query = \Mockery::mock(Builder::class);
$query->shouldReceive('limit')->with(2)->once()->andReturnSelf();

$limited = $this->modify($query, 2);
$this->assertSame($query, $limited);
}

public function modify($value, $limit)
{
return Modify::value($arr)->limit($limit)->fetch();
return Modify::value($value)->limit($limit)->fetch();
}
}

0 comments on commit 1caf2ad

Please sign in to comment.