File tree Expand file tree Collapse file tree 3 files changed +38
-0
lines changed
Expand file tree Collapse file tree 3 files changed +38
-0
lines changed Original file line number Diff line number Diff 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
5161Check if the post has a translation in default or given locale.
Original file line number Diff line number Diff line change 66use Astrotomic \Translatable \Traits \Scopes ;
77use Illuminate \Database \Eloquent \Collection ;
88use Illuminate \Database \Eloquent \Model ;
9+ use Illuminate \Database \Eloquent \ModelNotFoundException ;
910use 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);
Original file line number Diff line number Diff line change 1010use Astrotomic \Translatable \Tests \Eloquent \Vegetable ;
1111use Astrotomic \Translatable \Tests \Eloquent \VegetableTranslation ;
1212use Illuminate \Database \Eloquent \MassAssignmentException ;
13+ use Illuminate \Database \Eloquent \ModelNotFoundException ;
1314use Illuminate \Foundation \Testing \RefreshDatabase ;
1415use Illuminate \Support \Facades \App ;
1516use 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 {
You can’t perform that action at this time.
0 commit comments