Dimsav\Translatable\Translatable::scopeListsTranslations PHP Method

scopeListsTranslations() public method

Example usage: Country::listsTranslations('name')->get()->toArray() Will return an array with items: [ 'id' => '1', // The id of country 'name' => 'Griechenland' // The translated name ]
public scopeListsTranslations ( Builder $query, string $translationField )
$query Illuminate\Database\Eloquent\Builder
$translationField string
    public function scopeListsTranslations(Builder $query, $translationField)
    {
        $withFallback = $this->useFallback();
        $translationTable = $this->getTranslationsTable();
        $localeKey = $this->getLocaleKey();
        $query->select($this->getTable() . '.' . $this->getKeyName(), $translationTable . '.' . $translationField)->leftJoin($translationTable, $translationTable . '.' . $this->getRelationKey(), '=', $this->getTable() . '.' . $this->getKeyName())->where($translationTable . '.' . $localeKey, $this->locale());
        if ($withFallback) {
            $query->orWhere(function (Builder $q) use($translationTable, $localeKey) {
                $q->where($translationTable . '.' . $localeKey, $this->getFallbackLocale())->whereNotIn($translationTable . '.' . $this->getRelationKey(), function (QueryBuilder $q) use($translationTable, $localeKey) {
                    $q->select($translationTable . '.' . $this->getRelationKey())->from($translationTable)->where($translationTable . '.' . $localeKey, $this->locale());
                });
            });
        }
    }