@appnorth Show example and what relation it is. It's working just fine.
Feb 21, 2015
10
Level 2
[L4.2] distinct() and pagination returns total number of results not distinct total
Hi
I am wondering what the best solution to this problem is?
When running:
$posts = $company->posts()
->distinct()
->paginate(5);
The 'total' returned and used by the pagination equals the total number of posts (55) and not the distinct number of posts (22).
I have seen a number of posts online regarding the issue and there is mention of using DB:raw in the query but i'm unsure how to do that.
Any example or guidance very welcome.
Thanks
Barry.
Level 53
@appnorth First off a suggestion:
// this query is redundant and findOrFail makes no sense,
// since in case of FAIL you would first get Trying to get property of non-object on ->id part
$company = Company::findOrFail( // 2nd query for company
Auth::user()->company->id // 1st query for company
);
// If you want OrFail, then you may use this:
$company = Auth::user()->company()->firstOrFail(); // single query
Then, I suppose pasted data is irrelevant - client_id, project_id? Anyway, try this:
$company->clients()->groupBy('clients.id')->paginate();
3 likes
Please or to participate in this conversation.