From 7581c849c3af29ef40cf47a4d50c665e2805bd49 Mon Sep 17 00:00:00 2001 From: marijnz0r Date: Sat, 23 Feb 2019 12:35:56 +0100 Subject: [PATCH] Serialize Translator model using json_encode (#3894) * Add the feature to serialize the Translator model using json_encode * Update Translator.php --- composer.json | 3 ++- src/Translator.php | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 28b1b0d851..86d973047e 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "larapack/doctrine-support": "~0.1.4", "arrilot/laravel-widgets": "^3.7", "league/flysystem": "~1.0.41", - "larapack/voyager-hooks": "~1.0.5" + "larapack/voyager-hooks": "~1.0.5", + "ext-json": "*" }, "require-dev": { "phpunit/phpcov": "~3.0|~4.0", diff --git a/src/Translator.php b/src/Translator.php index 28e7ffdfcc..e0feb6a588 100644 --- a/src/Translator.php +++ b/src/Translator.php @@ -4,9 +4,10 @@ use ArrayAccess; use Illuminate\Database\Eloquent\Model; +use JsonSerializable; use TCG\Voyager\Facades\Voyager as VoyagerFacade; -class Translator implements ArrayAccess +class Translator implements ArrayAccess, JsonSerializable { protected $model; protected $attributes = []; @@ -316,4 +317,11 @@ public function runTranslatorMethod($method, array $arguments) return call_user_func_array([$this->model, $method], $arguments); } + + public function jsonSerialize() + { + return array_map(function ($array) { + return $array['value']; + }, $this->getRawAttributes()); + } }