为什么显示 Undefined array key "title" 错误?

Why it shows Undefined array key "title" error?

我有一个查询 returns 一个特定的 post:

  $post = Post::where('post_code', $post['post_code'])->first();

它 returns 像这样:

^ App\Models\Post {#1351 ▼
  #connection: "mysql"
  #table: "hotels"
  ...
  #attributes: array:6 [▼
    "id" => 1
    "post_code" => "AB1"
    "comments" => "[{"key": "1DfsGlyLv3yK1", "layout": "comment", "attributes": {"title": "title 1", "message": "comment 1"}}, {"key": lfhK5Nbwc2cPLKY", "layout": "note", "attributes": {"title": "title 2", "message": "comment 2"}}] ◀"
  ]

我想显示每个评论及其特定标题和评论,如下所示:

[
                    'name' => "post_comments",
                    'data' => [
                        'comments' =>
                        collect($post['comments'])->map(function ($comment) {
                            return [
                                'title' => $comment['title'],
                                'message' => $comment['message'],
                            ];
                        }),
                    ]
                ],

但是这样做会显示错误:

Undefined array key "title"

你知道标题键存在的问题是什么吗?谢谢

我看到你刚刚在评论对象中错过了一个级别:

[
                    'name' => "post_comments",
                    'data' => [
                        'comments' =>
                        collect($post['comments'])->map(function ($comment) {
                            return [
                                'title' => $comment['attributes']['title'],
                                'message' => $comment['attributes']['message'],
                            ];
                        }),
                    ]
                ],