PHP code example of spatie / laravel-json-api-paginate

1. Go to this page and download the library: Download spatie/laravel-json-api-paginate library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / laravel-json-api-paginate example snippets


'providers' => [
    ...
    Spatie\JsonApiPaginate\JsonApiPaginateServiceProvider::class,
];



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 key of the page[x] query string parameter for cursor.
     */
    'cursor_parameter' => 'cursor',

    /*
     * The name of the macro that is added to the Eloquent 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,

    /*
     * If you want to use cursor pagination, set this to true.
     * This would override use_simple_pagination.
     */
    'use_cursor_pagination' => false,

    /*
     * use simpleFastPaginate() or fastPaginate from https://github.com/hammerstonedev/fast-paginate
     * use may installed it via `composer 

YourModel::jsonPaginate();

YourModel::where('my_field', 'myValue')->jsonPaginate();

$model = YourModel::find(1);

$model->relation()->jsonPaginate();

$maxResults = 60;

YourModel::jsonPaginate($maxResults);

$defaultSize = 15;

YourModel::jsonPaginate(null, $defaultSize);

$total = 42;

YourModel::jsonPaginate(null, null, $total);



return [
    // ..... other config options .....

    /*
     * The key of the page[x] query string parameter for cursor.
     */
    'cursor_parameter' => 'cursor',

    /*
     * If you want to cursor pagination, set this to true.
     * This would override use_simple_pagination.
     */
    'use_cursor_pagination' => true,

    // ..... other config options .....
];

bash
php artisan vendor:publish --provider="Spatie\JsonApiPaginate\JsonApiPaginateServiceProvider" --tag="config"