Git Product home page Git Product logo

Comments (9)

sdebacker avatar sdebacker commented on August 30, 2024 1

Here is a cleaner method which use Laravel’s Str class for pluralization:

public function scopeTranslated($query)
{
    $translationTable = str_plural(snake_case(class_basename($this->getTranslationModelName())));
    $query->select()
        ->addSelect($this->getTable() . '.id as id')
        ->where('locale', App::getLocale())
        ->join(
            $translationTable,
            $this->getTable() . '.id',
            '=',
            $translationTable . '.' . $this->getRelationKey()
        );
    return $query;
}

from laravel-translatable.

sdebacker avatar sdebacker commented on August 30, 2024

I'm afraid you will have to use a join method.

from laravel-translatable.

maciekish avatar maciekish commented on August 30, 2024

Okay, i was hoping i could replace that code with something cleaner, but if there is nothing, maybe you should include this in laravel-translatable?

Here is my function, is there a way to make it infer the model name? Right now i have to use it like this

Category::translated('Category')->orderBy('name')->get();
public function scopeTranslated($query, $model) {
        $model = preg_replace('/([A-Z])/', '_$1', $model);
        $model = strtolower($model);
        if (substr($model, 0, 1) == '_')
            $model = substr($model, 1);
        $pos = strrpos($model, 'y');
        if ($pos == strlen($model) - 1)
            $model_plural = substr_replace($model, 'ie', $pos, strlen($model));
        else
            $model_plural = $model;
        return $query->join($model.'_translations', $model_plural.'s.id', '=', $model.'_translations.'.$model.'_id')->where($model.'_translations.locale', '=', App::getLocale());
    }

from laravel-translatable.

sdebacker avatar sdebacker commented on August 30, 2024

Ouch… give me five minuts…

from laravel-translatable.

maciekish avatar maciekish commented on August 30, 2024

Very nice, Ill test it later today. In my defense the code was written in a hurry :)

from laravel-translatable.

sdebacker avatar sdebacker commented on August 30, 2024

Yeah, give me feedback as I didn’t test this method a lot.

from laravel-translatable.

maciekish avatar maciekish commented on August 30, 2024

Seems to be working in all my uses cases. Thanks a lot for your help, PHP isn't my native language as you probably noticed :)

Definitely think this should be included in the package.

from laravel-translatable.

dimsav avatar dimsav commented on August 30, 2024

You could also use a more simple syntax because laravel already has this functionality:

Get translated records only

$countries = Country::has('translations')->get();

Get translated records in specific locale

$locale = 'en';
$countries = Country::whereHas('translations', function($q) use ($locale)
{
    $q->where('locale', '=', $locale);

})->get();

from laravel-translatable.

dimsav avatar dimsav commented on August 30, 2024

Scope added for version 5!

from laravel-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.