larahook / distinct-on-pagination
ORM Multiple distinct() with paginate()
v1.1.5
2024-04-02 10:03 UTC
Requires
- php: ^8.0
- laravel/framework: ^10.0|^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.51
README
Multiple distinct(['field_a', 'field_b'])
with paginate()
Problem
- ORM select return error when calculate
total
rows forpagination
SomeModel::select(['*']) ->distinct(['field_a', 'field_b']) ->orderBy('field_a') ->orderBy('field_b') ->paginate($perPage)
- Exception example
SQLSTATE[42883]: Undefined function: 7 ERROR: function count(bigint, bigint) does not exist\nLINE 1: select count(distinct \"field_a\", \"field_b\") as aggregate from \"...\n
Install
composer require larahook/distinct-on-pagination
Usage
- Add
DistinctOnPagination
trait inSomeModel
class - Pagination total must be calc without Exception
class SomeModel extends Model { use DistinctOnPagination; // <-- add DistinctOnPagination trait use HasFactory; /** @var string */ protected $table = 'some_table'; }
Config
- You can also publish the config file to change implementations (concat delimiter)
php artisan vendor:publish --provider="Larahook\DistinctOnPagination\DistinctOnPaginationServiceProvider" --tag=config