├── .github ├── FUNDING.yml └── workflows │ ├── php-cs-fixer.yml │ └── update-changelog.yml ├── .gitignore ├── .php-cs-fixer.dist.php ├── .styleci.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src ├── Facades │ └── PaginationMerge.php ├── PaginationMerge.php └── PaginationMergeServiceProvider.php └── tests └── PaginationMergeTest.php /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | custom: https://www.paypal.com/paypalme/aneeskhan47 4 | -------------------------------------------------------------------------------- /.github/workflows/php-cs-fixer.yml: -------------------------------------------------------------------------------- 1 | name: Check & fix styling 2 | 3 | on: [push] 4 | 5 | jobs: 6 | php-cs-fixer: 7 | runs-on: ubuntu-latest 8 | 9 | steps: 10 | - name: Checkout code 11 | uses: actions/checkout@v3 12 | with: 13 | ref: ${{ github.head_ref }} 14 | 15 | - name: Run PHP CS Fixer 16 | uses: docker://oskarstark/php-cs-fixer-ga 17 | with: 18 | args: --config=.php-cs-fixer.dist.php --allow-risky=yes 19 | 20 | - name: Commit changes 21 | uses: stefanzweifel/git-auto-commit-action@v4 22 | with: 23 | commit_message: Fix styling -------------------------------------------------------------------------------- /.github/workflows/update-changelog.yml: -------------------------------------------------------------------------------- 1 | name: "Update Changelog" 2 | 3 | on: 4 | release: 5 | types: [released] 6 | 7 | jobs: 8 | update: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Checkout code 13 | uses: actions/checkout@v3 14 | with: 15 | ref: main 16 | 17 | - name: Update Changelog 18 | uses: stefanzweifel/changelog-updater-action@v1 19 | with: 20 | latest-version: ${{ github.event.release.name }} 21 | release-notes: ${{ github.event.release.body }} 22 | 23 | - name: Commit updated CHANGELOG 24 | uses: stefanzweifel/git-auto-commit-action@v4 25 | with: 26 | branch: main 27 | commit_message: Update CHANGELOG 28 | file_pattern: CHANGELOG.md -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | composer.lock 3 | vendor 4 | tests/temp 5 | .idea 6 | .phpunit.result.cache 7 | .php-cs-fixer.cache -------------------------------------------------------------------------------- /.php-cs-fixer.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__ . '/src', 6 | __DIR__ . '/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR12' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'class_attributes_separation' => [ 30 | 'elements' => [ 31 | 'method' => 'one', 32 | ], 33 | ], 34 | 'method_argument_space' => [ 35 | 'on_multiline' => 'ensure_fully_multiline', 36 | 'keep_multiple_spaces_after_comma' => true, 37 | ], 38 | 'single_trait_insert_per_statement' => true, 39 | ]) 40 | ->setFinder($finder); 41 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | 3 | disabled: 4 | - single_class_element_per_statement 5 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-pagination-merge` will be documented in this file 4 | 5 | ## v1.0.3 - 2022-06-27 6 | 7 | **Full Changelog**: https://github.com/aneeskhan47/laravel-pagination-merge/compare/v1.0.2...v1.0.3 8 | 9 | ## v1.0.2 - 2022-06-27 10 | 11 | **Full Changelog**: https://github.com/aneeskhan47/laravel-pagination-merge/compare/v1.0.1...v1.0.2 12 | 13 | ## v1.0.1 - 2022-06-27 14 | 15 | **Full Changelog**: https://github.com/aneeskhan47/laravel-pagination-merge/compare/v1.0.0...v1.0.1 16 | 17 | ## 1.0.0 - 2022-07-26 18 | 19 | - initial release 20 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Anees Khan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![](https://banners.beyondco.de/Laravel%20Pagination%20Merge.png?theme=light&packageManager=composer+require&packageName=aneeskhan47%2Flaravel-pagination-merge&pattern=architect&style=style_1&description=Merge+multiple+laravel+paginate+instances&md=1&showWatermark=1&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg) 2 | 3 | # Pagination Merge for Laravel 5/6/7/8/9/10/11/12 4 | 5 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/aneeskhan47/laravel-pagination-merge.svg?style=flat-square)](https://packagist.org/packages/aneeskhan47/laravel-pagination-merge) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/aneeskhan47/laravel-pagination-merge.svg?style=flat-square)](https://packagist.org/packages/aneeskhan47/laravel-pagination-merge) 7 | [![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/aneeskhan47/laravel-pagination-merge/php-cs-fixer.yml?label=code%20style&style=flat-square)](https://github.com/aneeskhan47/laravel-pagination-merge/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain) 8 | [![Laravel Versions](https://img.shields.io/badge/Laravel-5.x%2C%206.x%2C%207.x%2C%208.x%2C%209.x%2C%2010.x%2C%2011.x%2012.x-brightgreen.svg?style=flat-square)]() 9 | 10 | A simple Laravel pagination merge package that allows you to merge multiple `->paginate()` instances. 11 | 12 | ### Some Background 13 | 14 | I had to deal with something like that in a project I was working on, where in one of the pages I had to display two types of publication paginated and sorted by the created_at field. In my case, it was a **Post** model and an **Event** Model (hereinafter referred to as publications). 15 | 16 | The only difference is I didn't want to get all the publications from the database and then merge and sort the results, as you can imagine it would rise a performance issue if we have hundreds of publications. 17 | 18 | So I figure out that it would be more convenient to paginate each model and only then, merge and sort them. that's why I built this package. 19 | 20 | This package is heavily inspired by this stackoverflow [answer](https://stackoverflow.com/a/58252907) 21 | 22 | ## ⚡️ Installation 23 | 24 | ### For Laravel 5.5+ 25 | 26 | Require this package with composer: 27 | 28 | ``` 29 | composer require aneeskhan47/laravel-pagination-merge 30 | ``` 31 | 32 | The service provider will be auto-discovered. You do not need to add the provider anywhere. 33 | 34 | ### For Laravel 5.0 to 5.4 35 | 36 | Require this package with composer: 37 | 38 | ``` 39 | composer require aneeskhan47/laravel-pagination-merge 40 | ``` 41 | 42 | Find the `providers` key in `config/app.php` and register the PaginationMerge Service Provider. 43 | 44 | ```php 45 | 'providers' => [ 46 | // ... 47 | Aneeskhan47\PaginationMerge\PaginationMergeServiceProvider::class, 48 | ] 49 | ``` 50 | 51 | Find the `aliases` key in `config/app.php` and register the PaginationMerge alias. 52 | 53 | ```php 54 | 'aliases' => [ 55 | // ... 56 | 'PaginationMerge' => Aneeskhan47\PaginationMerge\Facades\PaginationMerge::class, 57 | ] 58 | ``` 59 | 60 | ## 🚀 Usage 61 | 62 | ```php 63 | use App\Models\Post; 64 | use App\Models\Event; 65 | use Aneeskhan47\PaginationMerge\Facades\PaginationMerge; 66 | 67 | 68 | class PublicationsController extends Controller 69 | { 70 | /** 71 | * Display a listing of the resource. 72 | * 73 | * @param \Illuminate\Http\Request $request 74 | * @return \Illuminate\Http\Response 75 | */ 76 | public function index(Request $request) 77 | { 78 | $events = Event::latest()->paginate(5); 79 | $posts = Post::latest()->paginate(5); 80 | 81 | $publications = PaginationMerge::merge($events, $posts) 82 | ->sortByDesc('created_at') 83 | ->get(); 84 | 85 | // since get() will return \Illuminate\Pagination\LengthAwarePaginator 86 | // you can continue using paginator methods like these etc: 87 | 88 | $publications->withPath('/admin/users') 89 | ->appends(['sort' => 'votes']) 90 | ->withQueryString() 91 | ->fragment('users') 92 | ->setPageName('publications_page'); 93 | 94 | return view('publications.index', compact('publications')); 95 | } 96 | } 97 | ``` 98 | 99 | ## 💰 Support the development 100 | **Do you like this package? Support it by donating** 101 | 102 | - PayPal: [Donate](https://www.paypal.com/paypalme/aneeskhan47) 103 | 104 | ## 🧪 Testing 105 | 106 | ```bash 107 | composer test 108 | ``` 109 | 110 | ## 📝 Changelog 111 | 112 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 113 | 114 | ## 🤝 Contributing 115 | 116 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 117 | 118 | ## 🔒 Security 119 | 120 | If you discover any security-related issues, please email kingkhan2388@gmail.com instead of using the issue tracker. 121 | 122 | ## 🙌 Credits 123 | 124 | - [Anees Khan](https://github.com/aneeskhan47) 125 | - [All Contributors](../../contributors) 126 | 127 | ## 📜 License 128 | 129 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 130 | 131 | ## 🔧 Laravel Package Boilerplate 132 | 133 | This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). 134 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aneeskhan47/laravel-pagination-merge", 3 | "description": "Merge multiple laravel paginate instances", 4 | "keywords": [ 5 | "aneeskhan47", 6 | "laravel-pagination-merge" 7 | ], 8 | "homepage": "https://github.com/aneeskhan47/laravel-pagination-merge", 9 | "license": "MIT", 10 | "type": "library", 11 | "authors": [ 12 | { 13 | "name": "Anees Khan", 14 | "email": "kingkhan2388@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^7.2|^8.0|^8.1|^8.2", 20 | "illuminate/support": "~5|^6|^7|^8|^9|^10|^11.0|^12.0", 21 | "illuminate/pagination": "~5|^6|^7|^8|^9|^10|^11.0|^12.0" 22 | }, 23 | "require-dev": { 24 | "friendsofphp/php-cs-fixer": "^3.8", 25 | "orchestra/testbench": "^3.0|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0", 26 | "phpunit/phpunit": "^8.5|^9.0|^10.0|^11.0" 27 | }, 28 | "autoload": { 29 | "psr-4": { 30 | "Aneeskhan47\\PaginationMerge\\": "src" 31 | } 32 | }, 33 | "autoload-dev": { 34 | "psr-4": { 35 | "Aneeskhan47\\PaginationMerge\\Test\\": "tests" 36 | } 37 | }, 38 | "scripts": { 39 | "test": "vendor/bin/phpunit", 40 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 41 | }, 42 | "config": { 43 | "sort-packages": true 44 | }, 45 | "extra": { 46 | "laravel": { 47 | "providers": [ 48 | "Aneeskhan47\\PaginationMerge\\PaginationMergeServiceProvider" 49 | ], 50 | "aliases": { 51 | "PaginationMerge": "Aneeskhan47\\PaginationMerge\\Facades\\PaginationMerge" 52 | } 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | src/ 6 | 7 | 8 | 9 | 10 | tests 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/Facades/PaginationMerge.php: -------------------------------------------------------------------------------- 1 | total(); 51 | }, 0); 52 | 53 | $perPage = array_reduce($paginators, function ($carry, $paginator) { 54 | return $paginator->perPage(); 55 | }, 0); 56 | 57 | $items = array_map(function ($paginator) { 58 | return $paginator->items(); 59 | }, $paginators); 60 | 61 | $items = Arr::flatten($items); 62 | 63 | $items = Collection::make($items); 64 | 65 | $this->items = $items; 66 | $this->perPage = $perPage; 67 | $this->total = $total; 68 | 69 | return $this; 70 | } 71 | 72 | /** 73 | * Sort the collection using the given callback. 74 | * 75 | * @param callable|string $callback 76 | * @param int $options 77 | * @param bool $descending 78 | * @return $this 79 | */ 80 | public function sortBy($callback, $options = SORT_REGULAR, $descending = false) 81 | { 82 | $this->items = $this->items->sortBy($callback, $options, $descending); 83 | 84 | return $this; 85 | } 86 | 87 | /** 88 | * Sort the collection in descending order using the given callback. 89 | * 90 | * @param callable|string $callback 91 | * @param int $options 92 | * @return $this 93 | */ 94 | public function sortByDesc($callback, $options = SORT_REGULAR) 95 | { 96 | return $this->sortBy($callback, $options, true); 97 | } 98 | 99 | /** 100 | * Get merged paginator 101 | * 102 | * @return \Illuminate\Pagination\LengthAwarePaginator 103 | */ 104 | public function get() 105 | { 106 | return new LengthAwarePaginator( 107 | $this->items, 108 | $this->total, 109 | $this->perPage, 110 | LengthAwarePaginator::resolveCurrentPage(), 111 | [ 112 | 'path' => LengthAwarePaginator::resolveCurrentPath(), 113 | ] 114 | ); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /src/PaginationMergeServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('laravel-pagination-merge', function () { 24 | return new PaginationMerge(); 25 | }); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/PaginationMergeTest.php: -------------------------------------------------------------------------------- 1 | assertTrue(true); 12 | } 13 | } 14 | --------------------------------------------------------------------------------