jackardios / elastic-json-api-paginate
ElasticScoutDriverPlus 扩展,适用于 https://github.com/spatie/laravel-json-api-paginate
Requires
- php: ^7.3|^8.0
- illuminate/support: ^6.0|^7.0|^8.0
- jackardios/elastic-scout-driver-plus: ^3.0
- spatie/laravel-json-api-paginate: ^1.10
Requires (Dev)
- laravel/scout: ^9.3
- 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-12 13:37:58 UTC
README
本包仅是 ElasticScoutDriverPlus 的扩展,用于 spatie/laravel-json-api-paginate
在纯 Laravel 应用中,查询构建器的分页器将监听 page
请求参数。这非常好,但它遵循了 json:api 规范 的示例解决方案。该示例 期望 查询构建器的分页器监听 page[number]
和 page[size]
请求参数。
本包为 Scout 查询构建器添加了 jsonPaginate
方法,该方法监听这些参数,并添加了规范要求的 分页链接。
安装
您可以通过 composer 安装此包
composer require jackardios/elastic-json-api-paginate
在 Laravel 5.5 及以上版本中,服务提供器将自动注册。在框架的旧版本中,只需在 config/app.php
文件中添加服务提供器
'providers' => [ ... Spatie\JsonApiPaginate\JsonApiPaginateServiceProvider::class, Jackardios\ElasticJsonApiPaginate\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. * * THIS CONFIGURATION IS NOT SUPPORTED IN jackardios/elastic-json-api-paginate */ '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::searchQuery(...)->jsonPaginate();
当然,您仍然可以使用您所熟悉和喜爱的所有构建器方法
YourModel::searchQuery(...)->sort('my_field', 'desc')->jsonPaginate();
默认情况下,最大页面大小设置为 30。您可以在 config
文件中更改此数字,或者直接传递给 jsonPaginate
。
$maxResults = 60; YourModel::searchQuery(...)->jsonPaginate($maxResults);
默认情况下,ElasticScoutDriverPlus 分页原始结果,如果您想分页模型,请在 jsonPaginate
后调用 onlyModels
方法
YourModel::searchQuery(...)->jsonPaginate()->onlyModels();
测试
composer test
贡献
有关详细信息,请参阅 CONTRIBUTING
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件