/** * Find a model by it's url. * * @param string $url * @param string $locale * * @return \Illuminate\Database\Eloquent\Model */ public function findByUrl($url, $locale = null) { $locale = $locale ?: content_locale(); return $this->rememberForever("url.{$url}.{$locale}", function () use($url, $locale) { return $this->dbRepository->findByUrl($url, $locale); }); }
/** * @return \Illuminate\Database\Eloquent\Model|null */ public function findByUrl(string $url) { $model = static::MODEL; if (!isset((new $model())->translatedAttributes)) { return $this->query()->online()->where('url', $url)->first(); } return $this->query()->online()->whereTranslation('url', $url, content_locale())->first(); }
/** * Get a translated fragment's text. Since this utility function is occasionally used in route files, there's also a * check for the database connection to return a fallback fragment in local environments. * * @param string $locale * * @return \Spatie\String\Str | string */ function fragment(string $name, $locale = null) { $locale = $locale ?: content_locale(); $fragment = App\Models\Fragment::findByName($name); if (!$fragment) { return $name; } return string($fragment->getTranslation($locale)->text); }
/** * Get a translated fragment's text. Since this utility function is occasionally used in route files, there's also a * check for the database connection to return a fallback article in local environments. * * @param string $name * @param string $locale * * @return string */ function fragment($name, $locale = null) { $locale = $locale ?: content_locale(); $fragment = app(App\Repositories\FragmentRepository::class)->findByName($name); if (!$fragment) { return $name; } return $fragment->getTranslation($locale)->text; }
public static function findByNameOrCreate(string $name, TagType $type) : Tag { $existing = Tag::whereTranslation('name', $name, content_locale())->first(); if ($existing) { return $existing; } $tag = new static(['type' => $type, 'draft' => false, 'online' => true]); foreach (config('app.locales') as $locale) { $tag->translateOrNew($locale)->name = $name; } $tag->save(); return $tag; }
public static function findBySlug(string $slug) : Article { $article = Article::online()->where('slug->' . content_locale(), $slug)->first(); abort_unless($article, 404); return $article; }
function translate_field_name(string $name, string $locale = '') : string { $locale = $locale ?? content_locale(); return "translated_{$locale}_{$name}"; }
/** * Get the base database query. * * @return \Illuminate\Database\Eloquent\Builder */ public function query() { return $this->model->newQuery()->with(['media', 'translations' => function ($query) { $query->where('locale', content_locale()); }])->orderBy('order_column')->nonDraft(); }
/** * @return string */ public static function getDefaultTagLocale() { return content_locale(); }
protected function locale() : string { return content_locale(); }
/** * Get a tag from it's url. * * @param string $url * @param string $locale * * @return \App\Models\Tag */ public function findByUrl($url, $type = null, $locale = null) { $type = $type ?: HasTags::getDefaultTagType(); $locale = $locale ?: content_locale(); return $this->model->where('type', $type)->whereHas('translations', function ($query) use($url, $locale) { $query->where('url', $url)->where('locale', $locale); })->first(); }
public static function findBySlug(string $slug) : NewsItem { return NewsItem::online()->where('slug->' . content_locale(), $slug)->firstOrFail(); }