Skip to content

Commit

Permalink
Add scopeWithLocale
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgilles committed Sep 8, 2023
1 parent 85d1739 commit 08bb358
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ This trait add :
* A relation `translations` containing all translations of the model
* A relation `translationsWithDeleted` containing all translations of the model, including those in trash if your model use SoftDelete trait
* A function `translate(string $locale, array $translateAttributes = [])` to translate a model in a new locale
* A function `getTranslation(string $locale, bool $withDeleted = false)` returning the translated model in specified locale or null if it doesn't exist.
* A function `getTranslation(string $locale, bool $withDeleted = false)` returning the translated model in specified locale or null if it doesn't exist.
* A scope `withLocale($locale)` on query

```php
$post = new Post([
Expand Down
6 changes: 6 additions & 0 deletions src/Traits/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Novius\LaravelTranslatable\Traits;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\SoftDeletingScope;
Expand Down Expand Up @@ -77,6 +78,11 @@ public function translationsWithDeleted()
return $this->translations();
}

public function scopeWithLocale(Builder $query, ?string $locale): Builder
{
return $query->where('locale', $locale);
}

public function translate(string $locale, array $translateAttributes = []): static
{
$localeColumn = $this->getLocaleColumn();
Expand Down

0 comments on commit 08bb358

Please sign in to comment.