laravel-livewire-tables icon indicating copy to clipboard operation
laravel-livewire-tables copied to clipboard

Change formatting of relational column to allow proper sorting.

Open korki696 opened this issue 2 years ago • 10 comments

All Submissions:

  • [x] Have you followed the guidelines in our Contributing document?
  • [x] Have you checked to ensure there aren't other open Pull Requests for the same update/change?

New Feature Submissions:

  1. [x] Does your submission pass tests and did you add any new tests needed for your feature?
  2. [ ] Did you update all templates (if applicable)?
  3. [ ] Did you add the relevant documentation (if applicable)?
  4. Did you test locally to make sure your feature works as intended?

Changes to Core Features:

  • [x] Have you added an explanation of what your changes do and why you'd like us to include them?
  • [ ] Have you written new tests for your core changes, as applicable?
  • [x] Have you successfully ran tests with your changes locally?

When sorting a relational column, the table wouldn't actually sort. Changing the query to use a backquote (`) instead of a double quote (") seems to fix this.

korki696 avatar May 04 '22 19:05 korki696

We need a way for this to both work in MySQL and postgres. Personally, I can't replicate this though.

rappasoft avatar May 04 '22 19:05 rappasoft

I am running on MySQL8 and even just a raw query will only work with backquotes. I don't have a Postgres spun up or am to familiar with it. Are you able to sort a relational column on MySQL with the current implementation?

korki696 avatar May 04 '22 19:05 korki696

Should we do a conditional and just set it differently for the different drivers?

rappasoft avatar May 05 '22 01:05 rappasoft

Dear Anthony,

This pull request deals with the same problem I described in my reply to #734. As this pull request is just rewinding the changes in #734, I agree the query should be defined conditionally by the database driver being used.

Therefore, I made a change that uses the Laravel logic for query grammar. Maybe there is a simpeler approach, but I believe in this way, the proper alias/column is used and the quotes are set depending on the database driver. I haven't been able to test it for Postgresql, but for MySQL it works.

I wanted to create a pull request for it, but since this one is still open, I thought it might be better to put it here first. Please let me know if I need to create a pull request anyway.

https://github.com/ddejong98/laravel-livewire-tables/commit/ad96a80bcbc215ab06a41ee5a8eff02424301758

damiandejong avatar May 24 '22 13:05 damiandejong

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 23 '22 18:06 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 01 '22 22:08 stale[bot]

Facing the same issue. I had a column with relationship sorting. Everything was working fine for version 2.1.1. After upgrading to 2.2.0+, none of the related columns are working.

Seems like the problem is when order by column name is replaced with double quotes " instead of `, then sorting is stopped with MySQL.

mitulgolakiya avatar Aug 05 '22 13:08 mitulgolakiya

Yes, facing same issue.

after upgrading to latest version in MySQL, sorting is not working.

vishalinfyom avatar Aug 09 '22 06:08 vishalinfyom

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 08 '22 12:09 stale[bot]

I was able to resolve by adding this method to my controllers:

    public function getRows()
    {
        $this->baseQuery();
        $query = $this->getBuilder()->getQuery();
        $orders = $query->orders;
        $query->orders = [];

        foreach ($orders as $order) {
            $query->orders[] = [
                'type' => 'Raw',
                'sql' => str_replace('"', '`', $order['sql'])
            ];
        }
        $this->getBuilder()->setQuery($query);

        return $this->executeQuery();
    }

bskiefer avatar Sep 16 '22 13:09 bskiefer

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 16 '22 14:10 stale[bot]

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 22 '22 17:11 stale[bot]

Closing this in favor of this fix: https://github.com/ddejong98/laravel-livewire-tables/commit/ad96a80bcbc215ab06a41ee5a8eff02424301758

rappasoft avatar Dec 21 '22 02:12 rappasoft