distinct() with pagination() in laravel 5.2 not working

10,963

Solution 1

There seems to be some ongoing issues with Laravel and using distinct with pagination.

In this case, when pagination is determining the total number of records, it is ignoring the fields you have specified in your select() clause. Since it ignores your columns, the distinct functionality is ignored as well. So, the count query becomes select count(*) as aggregate from ...

To resolve the issue, you need to tell the paginate function about your columns. Pass your array of columns to select as the second parameter, and it will take them into account for the total count. So, if you do:

/*DB::stuff*/->paginate(5, ['T1.*']);

This will run the count query of:

select count(distinct T1.*) as aggregate from

So, your query should look like:

DB::table('myTable1 AS T1')
    ->select('T1.*')
    ->join('myTable2 AS T2','T2.T1_id','=','T1.id')
    ->distinct()
    ->paginate(5, ['T1.*']);

Solution 2

If it is possible, you can change distinct() with groupBy().

Solution 3

Use groupBy() instead of distinct(). I haven't tested but it will work. I was facing same issue in my query.

$author = Author::select('author_name')->groupBy('author_name')->paginate(15); 
Share:
10,963

Related videos on Youtube

AddWeb Solution Pvt Ltd
Author by

AddWeb Solution Pvt Ltd

AddWeb Solution - ISO and NASSCOM Certified Web Development IT company in Ahmedabad, India providing best quality and cost effective services to the clients across the globe Established: July 2012 Experience: 110+ Man Years with friendly - fun loving and responsible team Client: Small-Midsize Digital Agencies | Startups Techstack: PHP | Drupal | WordPress | Woo-Commerce| DevOps | Docker | Theme/ plugin development | Ionic | React Native [Android + iOS] | ReactJS | AngularJS | VueJS | Laravel | OctoberCMS | CoasterCMS | SilverStripe | YII | Cordova| CakePHP | CodeIgniter | PhoneGap | CiviCrm | Ajax | jQuery | Javascript | CSS | Mouf2 | .NET | CI-CD | GULP/ Grunt/ Bower | SASS | SCSS | LESS | Beacon | IoT | Drush | SSH | QA How We Work: We follow Agile methodology, by allocating dedicated member(s) Tools: Github | | Pantheon | Acquia | Slack | JIRA | Trello | Basecamp | BitBucket | Asana | Codebase | Teamwork | Vagrant | Asana | Docker Workflow we follow: - Sign NDA to ensure confidentiality and trust >> Analyse details >> Clarify doubts >> Setting up infrastructure >> Start execution >> Regular QA in every milestone >> Flawless outcomes For more information visit us: AddWebSolution | LinkedIN | Youtube | Facebook | Twitter | Sildshare | Codecanyon Review our partial work on Drupal: https://herbert-kohlmeyer.de [Drupal 8, Multi-Lingual, GULP, SASS, Search Functionality, Solr, SSL] http://sherpalearning.com/ [Drupal 7 Commerce, Slider, Payment Gateway, Parallax, Shipping, Facets, Coupons] Review our partial Wordpress Work: http://m1-order.com/ [WordPress, Digital Menu, Responsive, Contact Form 7, Webkit Transition] http://biologyskin.com/ [WordPress, Woo-Commerce, Social Media, Integration, Subscription, Payment Gateway] http://seniorsmatter.com/ [WordPress, Woo-Commerce, Multiple Payment Gateway, LESS, RBAC, Membership, Directory, Maps] Review our partial Laravel Work: http://el.addwebprojects.com/ [Laravel, Boilerplate, GULP, SASS, e-learning, Membership, Multiple Payment Gateway, Social Media-Integration] http://du.addwebprojects.com/ [Laravel, Recurring Payment, RBAC, Youtube Integration, Bootstrap] Review our partial Ionic Work: https://play.google.com/store/apps/details?id=com.mortgagespeak.greg&hl=en [ Categories, Audio Player] http://sageapp.addwebprojects.com/#/app/login [Ionic App, RBAC, Dashboard] [ User Credentials:- [email protected] /testing ]

Updated on September 15, 2022

Comments

  • AddWeb Solution Pvt Ltd
    AddWeb Solution Pvt Ltd over 1 year

    I'm trying to use distinct() with pagination() in laravel 5.2 with fluent and it's given result proper but pagination remain same(Like without apply distinct).

    I have already reviewed and tested below answers with mine code
    - laravel 5 - paginate total() of a query with distinct
    - Paginate & Distinct
    - Query Builder paginate method count number wrong when using distinct

    My code is something like:

    DB::table('myTable1 AS T1')
    ->select('T1.*')
    ->join('myTable2 AS T2','T2.T1_id','=','T1.id')
    ->distinct()
    ->paginate(5);
    

    EXAMPLE
    - I have result with three records(i.e. POST1, POST2, POST3 and POST1) so I apply distinct().
    - Now my result is POST1, POST2 and POST3 but pagination still display like 4 records(As result before applied distinct()).

    Any suggestion would be appreciated!

  • AddWeb Solution Pvt Ltd
    AddWeb Solution Pvt Ltd over 7 years
    Yes, this is the working solution and found it from it's origin..Thanks man..+1!
  • AddWeb Solution Pvt Ltd
    AddWeb Solution Pvt Ltd over 7 years
    Further I place my codejack(into my answer) to bypass the passing second param. Could you please suggest?
  • patricus
    patricus over 7 years
    You can't safely modify code inside the vendor directory. The next time you do a composer update, your change will get overwritten. This also adds extremely complexity to the ability to automate deployments, use CI, or even just simply manually moving your code from one place to another (you'll have to remember what custom changes you've made).
  • AddWeb Solution Pvt Ltd
    AddWeb Solution Pvt Ltd over 7 years
    @patricus: Yes I understand. Is there any way something like patch OR alter function(In Drupal/WP) which will prevent to discard my change?
  • lahwran
    lahwran over 3 years
    Hi Azwar! I think your answer's content is already covered by other answers. If you'd like to elaborate on those answers, I'd suggest doing so in a comment on one of them.