Last active
June 3, 2016 06:27
-
-
Save adumskis/1573856e53512486327a to your computer and use it in GitHub Desktop.
FillTrait for Laravel Traslatable models by fallback locale
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
use Illuminate\Support\Facades\Config; | |
trait FillTrait { | |
public static function create(array $attributes = []) | |
{ | |
$available_locales = Config::get('translatable.locales'); | |
$fallback_locale = Config::get('translatable.fallback_locale'); | |
$fallback_data = $attributes[$fallback_locale]; | |
foreach($available_locales as $available_locale) { | |
if ($available_locale !== $fallback_locale) { | |
if (isset($attributes[$available_locale])) { | |
$attributes[$available_locale] = array_merge($fallback_data, $attributes[$available_locale]); | |
} else { | |
$attributes[$available_locale] = $fallback_data; | |
} | |
} | |
} | |
return parent::create($attributes); | |
} | |
public function update(array $attributes = [], array $options = []) | |
{ | |
$available_locales = Config::get('translatable.locales'); | |
$fallback_locale = Config::get('translatable.fallback_locale'); | |
$fallback_data = $attributes[$fallback_locale]; | |
foreach($available_locales as $available_locale) { | |
if ($available_locale !== $fallback_locale) { | |
if (isset($attributes[$available_locale])) { | |
$attributes[$available_locale] = array_merge($fallback_data, $attributes[$available_locale]); | |
} else { | |
$attributes[$available_locale] = $fallback_data; | |
} | |
} | |
} | |
return parent::update($attributes, $options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment