laravel-datatables icon indicating copy to clipboard operation
laravel-datatables copied to clipboard

Fixes issue for ordering by a relationship.

Open Nathanw opened this issue 6 years ago • 3 comments

This fixes an issue when ordering by a HasMany relationship due to no selects being on the query and overriding the original models id with the id from the relationship.

Nathanw avatar Nov 27 '18 23:11 Nathanw

The PR looks good and tests are passing. Will check this asap.

BTW, if possible, can you add a test for this scenario? Thanks! 🍻

yajra avatar Nov 28 '18 01:11 yajra

@Nathanw I just realized after testing this that allowing sort on HasMany relationship would yield to multiple duplicate records since we are not grouping on sql level.

Duplicate records when sorted via User -> HasMany-> Post image

On the other hand, I think it does fix sorting on HasMany relationship.

Do you think we should merge this and allow the unnecessary duplicate results?

Any feedbacks from other users?

yajra avatar Jan 04 '19 02:01 yajra

@yajra Almost every relation cause duplicate results. It's always correct to groupBy the query inside your DataTable class manually. I encountered this problem over 100 times.

Nowadays I always include $query->groupBy('my_table'); just before I return the results.

I feel like this should be merged and groupBy thing should be mentioned as a "Good Practice" in the Docs.

OzanKurt avatar Oct 30 '19 20:10 OzanKurt