Skip to content

Commit

Permalink
Merge branch 'j6s-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
acasar committed May 15, 2017
2 parents 116c64c + 65801a8 commit 8d904d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,35 @@ public static function forceCreateInLocale($locale, array $attributes, $translat
});
}

/**
* Reload a fresh model instance from the database.
*
* @param array|string $with
* @return static|null
*/
public function fresh($with = [])
{
if (! $this->exists) {
return;
}

$query = static::newQueryWithoutScopes()
->with(is_string($with) ? func_get_args() : $with)
->where($this->getKeyName(), $this->getKey());

(new TranslatableScope())->apply($query, $this);

return $query->first();
}

/**
* @param array $translations
* @return bool
*/
public function saveTranslations(array $translations)
{
$success = true;
$fresh = $this->fresh();
$fresh = parent::fresh();

foreach($translations as $locale => $attributes) {
$model = clone $fresh;
Expand Down
8 changes: 8 additions & 0 deletions tests/TestCRUD.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ public function testSaveTranslationWithoutSideEffectsWhenUpdateModel()
$this->assertEquals('blog_cover.png', Post::first()->image);
}

public function testFreshReturnsTranslations()
{
$post = Post::forceCreate(['title' => 'Title 1']);
$post->title = 'Changed';

$this->assertEquals('Title 1', $post->fresh()->title);
}

public function testWhereTranslated()
{
Post::forceCreate(['title' => 'Title 1']);
Expand Down

0 comments on commit 8d904d6

Please sign in to comment.