From 90e5a41dca5d48dfa09006718a68a9d6be596f57 Mon Sep 17 00:00:00 2001 From: Alex Sofronie Date: Thu, 20 Jul 2017 21:09:53 +0300 Subject: [PATCH] fix #32 --- src/UuidBinaryModelTrait.php | 6 +++++- tests/EloquentUuidTest.php | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/UuidBinaryModelTrait.php b/src/UuidBinaryModelTrait.php index 35c52e7..168e426 100644 --- a/src/UuidBinaryModelTrait.php +++ b/src/UuidBinaryModelTrait.php @@ -101,7 +101,11 @@ private function deepArray($array) $useOptimization = !empty($this->uuidOptimization); foreach ($array as $key => $value) { if (!is_string($value)) { - $array[$key] = $this->deepArray((array)$value); + if (is_object($value) && method_exists($value, 'toArray')) { + $array[$key] = $value->toArray(); + } else { + $array[$key] = $this->deepArray((array)$value); + } } elseif (!ctype_print($value)) { $array[$key] = $useOptimization ? self::toNormal($value) : bin2hex($value); } diff --git a/tests/EloquentUuidTest.php b/tests/EloquentUuidTest.php index 92bc99b..217a508 100644 --- a/tests/EloquentUuidTest.php +++ b/tests/EloquentUuidTest.php @@ -515,6 +515,23 @@ public function testJsonCustomAtributesBinary() static::assertNotNull($json); } + public function testToArray() + { + $model = EloquentBinOptimizedUserModel::create([ + 'username' => 'Model', + 'password' => 'secret' + ]); + $customModel = EloquentBinOptimizedPostModel::create(['name' => 'bla']); + $model->cust = $customModel; + + $array = $model->toArray(); + static::assertNotNull($array); + + $json = json_encode($array); + var_dump($json); + static::assertNotNull($json); + } + /** * Bootstrap Eloquent. *