/** * Find by slug on translated attribute * * @param string $slug * @param string $locale optional, if omitted returns for any locale * @return $this|null */ public static function findBySlug($slug, $locale = null) { /** @var \Dimsav\Translatable\Translatable $model */ $model = new static(); /** @var SluggableTrait $translationModel */ $translationModel = $model->getTranslationModelName(); $parentKey = $model->getRelationKey(); $translationInstance = $translationModel::findBySlug($slug, $locale); if (empty($translationInstance)) { return null; } return static::find($translationInstance->{$parentKey}); }
/** * Inner join with the translation table. * * @param \Illuminate\Database\Eloquent\Builder $query * @param null $locale * * @return this */ public static function scopeJoinTranslation(Builder $query, $locale = null) { $instance = new static(); $translationModelName = $instance->getTranslationModelName(); $translationModel = new $translationModelName(); if (is_null($locale)) { $locale = $instance->locale(); } return $query->join($translationModel->getTable() . ' as t', 't.' . $instance->getRelationKey(), '=', $instance->getTable() . '.' . $instance->getKeyName())->where($instance->getLocaleKey(), $locale); }