如何在Laravel Blade中显示嵌入式MongoDB文档?

时间:2017-03-09 16:48:34

标签: mongodb laravel blade

大家好我想在本文档的laravel刀片中显示所有者名称。

我可以使用foreach循环显示名称和描述,但不能从嵌入文档中显示。

{
        "_id" : ObjectId("58c1b173ebda1297b747271b"),
        "name" : "Name of Board",
        "description" : "Board short description",
        "postdate" : ISODate("2016-12-19T06:01:17.171Z"),
        "owner" : [
                {
                        "_id" : ObjectId("58c13ebaebda1297b747271a"),
                        "name" : "Joker"
                }
        ],
        "tag" : {

        },
        "subscribers" : [
                {

                }
        ],
        "likes" : [
                ObjectId("58c13ebaebda1297b747271a")
        ]
}

请帮忙。提前谢谢你

2 个答案:

答案 0 :(得分:0)

作为它的嵌套数组,您需要使用foreach ...

访问它
@foreach ($boards as $board)
    <p>{{ $board->name }}</p>
    @foreach ($board->owner as $ownerArray)
    <p>{{ $ownerArray['name'] }}</p>
    @endforeach
  @endforeach

答案 1 :(得分:0)

确定。我改变了存储数据的方式。

{
        "_id" : ObjectId("58c1d1a48c2201423599f7fd"),
        "name" : "Name of Board2",
        "description" : "Board short description",
        "postdate" : ISODate("2016-12-19T06:01:17.171Z"),
        "owner" : {
                "_id" : ObjectId("58c13ebaebda1297b747271a"),
                "name" : "Joker"
        },
        "tag" : {

        },
        "subscribers" : [
                {

                }
        ],
        "likes" : [
                ObjectId("58c13ebaebda1297b747271a")
        ]
}

然后我通过

访问它
@foreach ($boards as $board)
    <p>{{ $board->name }}</p>

    <p>{{ $board['owner']['name'] }}</p>

  @endforeach