Skip to content

Define default limit #159

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

Merged
merged 7 commits into from
Apr 7, 2025
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
6 changes: 4 additions & 2 deletions src/Concerns/Resource/Paginable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ trait Paginable
*/
public function paginate($query, RestRequest $request)
{
$defaultLimit = $this->defaultLimit ?? 50;

// In case we have a scout builder
if ($query instanceof Builder) {
return $query->paginate($request->input('search.limit', 50), 'page', $request->input('search.page', 1));
return $query->paginate($request->input('search.limit', $defaultLimit), 'page', $request->input('search.page', 1));
}

return $query->paginate($request->input('search.limit', 50), ['*'], 'page', $request->input('search.page', 1));
return $query->paginate($request->input('search.limit', $defaultLimit), ['*'], 'page', $request->input('search.page', 1));
}
}
7 changes: 7 additions & 0 deletions src/Console/stubs/resource.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class {{ class }} extends RestResource
*/
public static $model = \{{ namespacedModel }}::class;

/**
* The default value for the pagination limit.
*
* @var int
*/
public int $defaultLimit = 50;

/**
* The exposed fields that could be provided
* @param RestRequest $request
Expand Down
7 changes: 7 additions & 0 deletions src/Console/stubs/user-resource.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class UserResource extends RestResource
*/
public static $model = \App\User::class;

/**
* The default value for the pagination limit.
*
* @var int
*/
public int $defaultLimit = 50;

/**
* The exposed fields that could be provided
* @param RestRequest $request
Expand Down
7 changes: 7 additions & 0 deletions src/Http/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ class Resource implements \JsonSerializable
*/
public static $response = Response::class;

/**
* The default value for the pagination limit.
*
* @var int
*/
public int $defaultLimit = 50;

/**
* Get a fresh instance of the model represented by the resource.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Query/ScoutBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public function search(array $parameters = [])
$this->applyInstructions($parameters['instructions']);
});

$this->queryBuilder->take($parameters['limit'] ?? 50);
$defaultLimit = $this->resource->defaultLimit ?? 50;
$this->queryBuilder->take($parameters['limit'] ?? $defaultLimit);

$this->queryBuilder
->query(function (Builder $query) use ($parameters) {
Expand Down
18 changes: 18 additions & 0 deletions tests/Feature/Controllers/SearchPaginateOperationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,22 @@ public function test_getting_a_list_of_resources_paginating_with_many_records():
new ModelResource()
);
}

public function test_to_get_a_list_of_paginated_resources_from_the_default_limit(): void
{
ModelFactory::new()->count(100)->create()->fresh();

Gate::policy(Model::class, GreenPolicy::class);

$response = $this->post(
'/api/model-with-default-limit/search',
[],
['Accept' => 'application/json']
);

$response->assertStatus(200);
$response->assertJsonPath('per_page', 32);
$response->assertJsonPath('last_page', 4);
$response->assertJsonCount(32, 'data');
}
}
11 changes: 11 additions & 0 deletions tests/Support/Http/Controllers/ModelWithDefaultLimitController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Lomkit\Rest\Tests\Support\Http\Controllers;

use Lomkit\Rest\Http\Controllers\Controller;
use Lomkit\Rest\Tests\Support\Rest\Resources\ModelWithDefaultLimitResource;

class ModelWithDefaultLimitController extends Controller
{
public static $resource = ModelWithDefaultLimitResource::class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Lomkit\Rest\Tests\Support\Rest\Resources;

class ModelWithDefaultLimitResource extends ModelResource
{
public int $defaultLimit = 32;
}
1 change: 1 addition & 0 deletions tests/Support/Routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
\Lomkit\Rest\Facades\Rest::resource('searchable-models', \Lomkit\Rest\Tests\Support\Http\Controllers\SearchableModelController::class);
\Lomkit\Rest\Facades\Rest::resource('model-hooks', \Lomkit\Rest\Tests\Support\Http\Controllers\ModelHooksController::class)->withSoftDeletes();
\Lomkit\Rest\Facades\Rest::resource('model-withs', \Lomkit\Rest\Tests\Support\Http\Controllers\ModelWithController::class);
\Lomkit\Rest\Facades\Rest::resource('model-with-default-limit', \Lomkit\Rest\Tests\Support\Http\Controllers\ModelWithDefaultLimitController::class);
\Lomkit\Rest\Facades\Rest::resource('no-relationship-authorization-models', \Lomkit\Rest\Tests\Support\Http\Controllers\NoRelationshipAuthorizationModelController::class);

\Lomkit\Rest\Facades\Rest::resource('no-exposed-fields', \Lomkit\Rest\Tests\Support\Http\Controllers\NoExposedFieldsController::class);
Expand Down