Giter VIP home page Giter VIP logo

Comments (6)

mrmonat avatar mrmonat commented on June 1, 2024 6

Here is a trait, that is a possible solution for the problem:

<?php

namespace App\Nova\Traits;

trait SortTranslatableFields
{
    protected static function applyOrderings($query, array $orderings)
    {
        $locale = app()->getLocale();
        $translatableAttributes = app(self::$model)->make()->getTranslatableAttributes();

        foreach ($orderings as $column => $direction) {
            if (in_array($column, $translatableAttributes)) {
                unset($orderings[$column]);
                $query->orderByRaw("`{$column}` -> '$.{$locale}' {$direction}");
            }
        }

        return parent::applyOrderings($query, $orderings);
    }
}

Maybe this Trait could be added to the package, but you have to manually "use" it in every Nova resource, where you use Translatable fields.

from nova-translatable.

tanthammar avatar tanthammar commented on June 1, 2024 2

I made a macro. That way I can set it once and forget about it.

use Spatie\Translatable\HasTranslations;
use Illuminate\Database\Eloquent\Builder;


Builder::macro('orderBy', function ($field, $order = 'asc', $locale = null) {
            if (
                in_array(HasTranslations::class, class_uses($this->model))
                && in_array($field, $this->model->translatable)
            ) {
                $locale = $locale ?? app()->getLocale();
                $field .= '->' . $locale;
                $this->query->orderBy($field, $order);
            } else {
                $this->query->orderBy($field, $order);
            }
            return $this;
        });

from nova-translatable.

freekmurze avatar freekmurze commented on June 1, 2024

Feel free to send a PR that fixes this

from nova-translatable.

mrmonat avatar mrmonat commented on June 1, 2024

I think this has nothing to do with this package, the problem is how Nova handles the underlying json columns when sorting / searching.

MySQL isn't able to sort based on json column values with a simple ORDER BY column_name ASC/DESC.

The query should look something like this to work properly:
ORDER BY `column` -> '$."en"' DESC

from nova-translatable.

freekmurze avatar freekmurze commented on June 1, 2024

Closing this as it can't be handled in the package.

from nova-translatable.

frtess avatar frtess commented on June 1, 2024

Here is a trait, that is a possible solution for the problem:

<?php

namespace App\Nova\Traits;

trait SortTranslatableFields
{
    protected static function applyOrderings($query, array $orderings)
    {
        $locale = app()->getLocale();
        $translatableAttributes = app(self::$model)->make()->getTranslatableAttributes();

        foreach ($orderings as $column => $direction) {
            if (in_array($column, $translatableAttributes)) {
                unset($orderings[$column]);
                $query->orderByRaw("`{$column}` -> '$.{$locale}' {$direction}");
            }
        }

        return parent::applyOrderings($query, $orderings);
    }
}

Maybe this Trait could be added to the package, but you have to manually "use" it in every Nova resource, where you use Translatable fields.

For those who land here, trying to go the Trait way, on PostgreSQL, here is the syntax to access your JSON fields:
$query->orderByRaw("{$column}->>'{$locale}' {$direction}");
You may have to play with -> and ->> after the column name, or cast to string after accessing with ->

from nova-translatable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.