-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
column-sortable not working with withCount() #49
Comments
Hi! Please try aliasing. https://github.com/Kyslik/column-sortable#sortableas-aliasing in your case it should be something like:
As I am looking at this right now I will improve this, you may try aliasing but if that renders any error just report it back here and stick with overloading. I will look at this since 5.4 is |
I tried aliasing before but obviously did something wrong.
in the Entry model did the trick and is slightly easier than overloading.
Thanks! |
I will update readme for L5.4, thanks! |
Hi, I'm on Laravel 5.5 and I have an issue. |
Hey @maurosbu I do not think I can help you out (no time); only if you can reproduce this on L5.8. I will try to solve it & port the solution to the all versions of this package. Make a fork of https://github.com/Kyslik/column-sortable-example and just make a PR with your reproduced bug. |
Hey @Kyslik, can you give a look at https://github.com/maurosbu/column-sortable-example/ Thanks, |
Hi, the columns selected in the query are overwritten with select($parentTable.'.*') in the private function formJoin of Sortable.php i think it must be done only if the columns is null `private function formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey)
what do you think ? |
This should be incorporated in the code because any custom $query->select(...) doesn't work owing to this overriding.
|
I have the same problem, when sorting for a related table which may have nulls, the count is lost. The fix proposed by @stephaned-exatech solves, after years can we consider it valid and do a PR? private function formJoin($query, $parentTable, $relatedTable, $parentPrimaryKey, $relatedPrimaryKey)
{
$joinType = config('columnsortable.join_type', 'leftJoin');
if ($query->getQuery()->columns === null) {
$query->select($parentTable.'.*');
}
return $query->{$joinType}($relatedTable, $parentPrimaryKey, '=', $relatedPrimaryKey);
} |
@Kyslik can you please fix with the proposed solution? |
This package seems not to work correctly with the new laravel function
withCount()
.I have models "Entry" and "Vote". One Entry can have many Votes, so in Entry
Entry::withCount('votes')
returns the number of votes correctly invotes_count
, but trying to sort byvotes_count
(via sortable) results in an error:I was able to make it work by overloading the sort for
"votes_count"
:(simply making sure the name of the order column is only
votes_count
without a table name).The text was updated successfully, but these errors were encountered: