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!
The distinct is working.... When you make a join, it checks for distinct entries in all selected columns and here.. the column names you get after the join are also included in the join...
To run this please add
select()
to your builder instance and it should work :)Let me know if this doesn't work for you :)