Skip to content

Commit 5203dbf

Browse files
authored
Merge pull request #114 from rb1193/master
Added translateOrFail method to translatable trait
2 parents 4942379 + 8954b06 commit 5203dbf

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

docs/usage/methods.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ $post->translateOrNew('fr'); // returns the french translation model
4646
$post->translateOrNew('it'); // returns the new italian translation model
4747
```
4848

49+
### translateOrFail\(string $locale\)
50+
51+
**Alias of:** `getTranslationOrFail(string $locale)`
52+
53+
This returns an instance of `PostTranslation` using the given locale and will throw a ModelNotFoundException if none exists.
54+
55+
```php
56+
$post->translateOrFail('fr'); // returns the french translation model
57+
```
58+
4959
## hasTranslation\(?string $locale = null\)
5060

5161
Check if the post has a translation in default or given locale.

src/Translatable/Translatable.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Astrotomic\Translatable\Traits\Scopes;
77
use Illuminate\Database\Eloquent\Collection;
88
use Illuminate\Database\Eloquent\Model;
9+
use Illuminate\Database\Eloquent\ModelNotFoundException;
910
use Illuminate\Support\Str;
1011

1112
/**
@@ -238,6 +239,15 @@ public function getTranslationOrNew(?string $locale = null): Model
238239
return $translation;
239240
}
240241

242+
public function getTranslationOrFail(string $locale): Model
243+
{
244+
if (($translation = $this->getTranslation($locale, false)) === null) {
245+
throw new ModelNotFoundException();
246+
}
247+
248+
return $translation;
249+
}
250+
241251
public function getTranslationsArray(): array
242252
{
243253
$translations = [];
@@ -317,6 +327,11 @@ public function translateOrNew(?string $locale = null): Model
317327
return $this->getTranslationOrNew($locale);
318328
}
319329

330+
public function translateOrFail(string $locale): Model
331+
{
332+
return $this->getTranslationOrFail($locale);
333+
}
334+
320335
protected function getLocalesHelper(): Locales
321336
{
322337
return app(Locales::class);

tests/TranslatableTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Astrotomic\Translatable\Tests\Eloquent\Vegetable;
1111
use Astrotomic\Translatable\Tests\Eloquent\VegetableTranslation;
1212
use Illuminate\Database\Eloquent\MassAssignmentException;
13+
use Illuminate\Database\Eloquent\ModelNotFoundException;
1314
use Illuminate\Foundation\Testing\RefreshDatabase;
1415
use Illuminate\Support\Facades\App;
1516
use Illuminate\Support\Facades\DB;
@@ -414,6 +415,18 @@ public function it_has_methods_that_return_always_a_translation(): void
414415
static::assertEquals('xyz', $vegetable->translateOrNew()->locale);
415416
}
416417

418+
/** @test */
419+
public function it_throws_an_exception_if_translation_does_not_exist(): void
420+
{
421+
$vegetable = Vegetable::create([
422+
'en' => ['name' => 'Peas'],
423+
]);
424+
static::assertEquals('en', $vegetable->translateOrFail('en')->locale);
425+
426+
$this->expectException(ModelNotFoundException::class);
427+
$vegetable->translateOrFail('xyz');
428+
}
429+
417430
/** @test */
418431
public function it_returns_if_attribute_is_translated(): void
419432
{

0 commit comments

Comments
 (0)