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.x] Make limit modifier work with query builders #10818

Merged
merged 5 commits into from
Sep 18, 2024
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
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();
}
}
Loading