diff --git a/framework/core/src/Api/Schema/Relationship/ToMany.php b/framework/core/src/Api/Schema/Relationship/ToMany.php index 24b7778ff..418338e3b 100644 --- a/framework/core/src/Api/Schema/Relationship/ToMany.php +++ b/framework/core/src/Api/Schema/Relationship/ToMany.php @@ -10,9 +10,22 @@ namespace Flarum\Api\Schema\Relationship; use Flarum\Api\Schema\Concerns\FlarumRelationship; +use InvalidArgumentException; +use Tobyz\JsonApiServer\Context; use Tobyz\JsonApiServer\Laravel\Field\ToMany as BaseToMany; class ToMany extends BaseToMany { use FlarumRelationship; + + public function serializeValue($value, Context $context): mixed + { + if ($value && ! is_array($value) && method_exists($value, 'toArray')) { + $value = $value->toArray(); + } elseif ($value && ! is_array($value)) { + throw new InvalidArgumentException(sprintf('Relationship value [%s] must be an array', $this->name)); + } + + return parent::serializeValue($value, $context); + } }