如何优化这种数据结构?

API result:

API result

php laravel

the article_user and article_title is repeated.

i use transformer to transform my data.

  public function transform(ArticleComment $comments)
  {
    $articleInfo = $comments->article;
    $user = UserInfo::select('real_name')->find($articleInfo->created_user_id);

    return [
        'article_user'             => $user->real_name,
        'article_title'            => $articleInfo->title,
        'is_evaluator'             => $comments->is_evaluation,
        'comment_created_user'     => $comments->user,
        'created_at'               => $comments->created_at,
        'comment_content'          => $comments->content,
        'replied_comment'          => $comments->reply,
        'replied_user'             => $comments->repliedUser,
      ];
  }
}

i want it like this

article_info:{
  article_user: "",
  article_title: "",
}
article_comment:{
  content: "",
  is_evaluator: 0,
  ...
}

how to optimize it ? what should i do ?

the response

You can use simple arrays to format your response. In your case it should be:

public function transform(ArticleComment $comments)
{
    $articleInfo = $comments->article;
    $user = UserInfo::select('real_name')->find($articleInfo->created_user_id);

    return [
        'article_info'    => [
            'article_user'  => $user->real_name,
            'article_title' => $articleInfo->title
        ],
        'article_comment' => [
            'is_evaluator'         => $comments->is_evaluation,
            'comment_created_user' => $comments->user,
            'created_at'           => $comments->created_at,
            'comment_content'      => $comments->content,
            'replied_comment'      => $comments->reply,
            'replied_user'         => $comments->repliedUser
        ]
    ];
}