jackardios / scout-json-api-paginate
扩展自 https://github.com/spatie/laravel-json-api-paginate
dev-master
2021-10-06 05:13 UTC
Requires
- php: ^7.3|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
- laravel/scout: ^8.0|^9.0
- spatie/laravel-json-api-paginate: ^1.10
Requires (Dev)
- orchestra/testbench: ^4.0|^5.0|^6.0
- phpunit/phpunit: ^8.0|^9.0
- roave/security-advisories: dev-latest
This package is auto-updated.
Last update: 2024-09-06 11:43:31 UTC
README
此包是 Scout 扩展,用于 spatie/laravel-json-api-paginate
在纯Laravel应用程序中,查询构建器的分页器将监听 page 请求参数。这很好,但它确实遵循了 json:api 规范 的例子。该例子 期望 查询构建器分页器监听 page[number] 和 page[size] 请求参数。
此包为Scout查询构建器添加了一个 jsonPaginate 方法,它监听这些参数并添加规范所需的 分页链接。
安装
您可以通过composer安装此包
composer require jackardios/scout-json-api-paginate
Laravel 5.5及更高版本中,服务提供程序将自动注册。在框架的较旧版本中,只需将服务提供程序添加到 config/app.php 文件中
'providers' => [ ... Spatie\JsonApiPaginate\JsonApiPaginateServiceProvider::class, Jackardios\ScoutJsonApiPaginate\JsonApiPaginateServiceProvider::class, ];
可选地,您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Spatie\JsonApiPaginate\JsonApiPaginateServiceProvider" --tag="config"
这是将要发布到 config/json-api-paginate.php 的文件内容
<?php return [ /* * The maximum number of results that will be returned * when using the JSON API paginator. */ 'max_results' => 30, /* * The default number of results that will be returned * when using the JSON API paginator. */ 'default_size' => 30, /* * The key of the page[x] query string parameter for page number. */ 'number_parameter' => 'number', /* * The key of the page[x] query string parameter for page size. */ 'size_parameter' => 'size', /* * The name of the macro that is added to the Scout query builder. */ 'method_name' => 'jsonPaginate', /* * If you only need to display Next and Previous links, you may use * simple pagination to perform a more efficient query. */ 'use_simple_pagination' => false, /* * Here you can override the base url to be used in the link items. */ 'base_url' => null, /* * The name of the query parameter used for pagination */ 'pagination_parameter' => 'page', ];
使用方法
要按json API规范分页结果,只需调用 jsonPaginate 方法。
YourModel::search('...')->jsonPaginate();
当然,您仍然可以使用您所熟悉和喜爱的所有构建器方法
YourModel::search('...')->where('my_field', 'myValue')->jsonPaginate();
默认情况下,最大页大小设置为30。您可以在 config 文件中更改此数字,或者直接传递给 jsonPaginate。
$maxResults = 60; YourModel::search('...')->jsonPaginate($maxResults);
测试
composer test
贡献
有关详细信息,请参阅 CONTRIBUTING
许可协议
MIT许可协议(MIT)。有关更多信息,请参阅 许可文件