Skip to content

orderByTranslation() loads ALL translations #458

@ryanbadger

Description

@ryanbadger

When trying to use orderByTranslation() it eager loads every field from every translation.

Found here:

Ideally, we'd have the option to limit it to the current locale.

Is this possible?

As a workaround, I'm having to do something like this:

 /**
     * Apply sorting for translatable fields
     */
    protected static function applyTranslatableSort(QueryBuilder $query, string $sortField, bool $isDesc): void
    {
        $direction = $isDesc ? 'desc' : 'asc';
        $locale = getCurrentLocale();

        $query->getEloquentBuilder()
            ->select('blog_categories.*')
            ->join('blog_category_translations AS sort_trans', function ($join) use ($locale) {
                $join->on('blog_categories.id', '=', 'sort_trans.blog_category_id')
                    ->where('sort_trans.locale', '=', $locale);
            })
            ->orderBy("sort_trans.{$sortField}", $direction);
    }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions