Skip to content

[Bug]: Making a column sortable doesn't work for translated columns #7

@Jelmerkeij

Description

@Jelmerkeij

What happened?

Using the GalaxyStore demo as an example here. In ProductResource.php::table() (line 241) set the translation to sortable:

Tables\Columns\TextColumn::make('translation.title')->sortable(),

When sorting in the table this doesn't do anything and I think it has to do with the query that's being built. Using Laravel debug bar we can see the query that's being generated:

SELECT
    *
FROM
    `products`
ORDER BY
    (
    SELECT
        `title`
    FROM
        `product_translations`
    INNER JOIN(
        SELECT
            MAX(`product_translations`.`id`) AS `id_aggregate`,
            MIN(
                `product_translations`.`product_id`
            ) AS `product_id_aggregate`,
            `product_translations`.`product_id`
        FROM
            `product_translations`
        INNER JOIN(
            SELECT
                MAX(
                    `product_translations`.`product_id`
                ) AS `product_id_aggregate`,
                `product_translations`.`product_id`
            FROM
                `product_translations`
            WHERE
                `locale` = 'en' AND `product_translations`.`product_id` IS NULL AND `product_translations`.`product_id` IS NOT NULL
            GROUP BY
                `product_translations`.`product_id`
        ) AS `translation`
    ON
        `translation`.`product_id_aggregate` = `product_translations`.`product_id` AND `translation`.`product_id` = `product_translations`.`product_id`
    WHERE
        `locale` = 'en'
    GROUP BY
        `product_translations`.`product_id`
    ) AS `translation`
ON
    `translation`.`id_aggregate` = `product_translations`.`id` AND `translation`.`product_id_aggregate` = `product_translations`.`product_id` AND `translation`.`product_id` = `product_translations`.`product_id`
WHERE
    `products`.`id` = `product_translations`.`product_id`
)
ASC
LIMIT 10 OFFSET 0;

The part that seems to cause the problem is:

`locale` = 'en' AND `product_translations`.`product_id` IS NULL AND `product_translations`.`product_id` IS NOT NULL

Stating that the product_id must be NULL AND also be NOT NULL at the same time. This never returns any record of course so there's nothing to sort on.

As a workaround I've added a custom sortable like this:

Tables\Columns\TextColumn::make('translation.title')
                    ->sortable(query: function (Builder $query, string $direction): Builder {
                    // Custom sortable to make it work with translation
                    return $query->orderByTranslation('title', $direction);
                }),

How to reproduce the bug

  1. Checkout the GalaxyStore demo from https://github.com/CactusGalaxy/GalaxyStoreExample
  2. In ProductResource.php::table() (line 241) set the translation to sortable: Tables\Columns\TextColumn::make('translation.title')->sortable(),
  3. See the sorting not doing anything on the frontend and check the query to see it's generating a IS NULL AND IS NOT NULL paradox

Package Version

0.2.1

PHP Version

8.3.0

Laravel Version

10.48.8

Which operating systems does with happen with?

macOS

Notes

No response

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions