【发布时间】:2017-12-28 17:14:03
【问题描述】:
我们正在使用 clojure 和 mongodb 构建一个庞大的系统。我正在为系统开发一个管理面板。但我使用的是 Laravel 5.4(最新版本)。
我设法将 mongodb 与 php 和 laravel 连接起来。
这是我的问题。 我们的数据库有多层数组。
这是数据库
/* 1 */
{
"_id" : ObjectId("58def7eb06703609d370f76f"),
"description" : "Tuxido",
"publisher" : ObjectId("578bb0f0e403c10f2f490d8f"),
"title" : "Tux",
"label" : "Tux",
"imgs" : [
{
"url" : "http://localhost:8088/ad/578bb0f0e403c10f2f490d8f-849ca60b06b9f79e337dd29f51f7ed384f205d1e46c68a19af6f3935c167d15a.png"
}
]
}
/* 2 */
{
"_id" : ObjectId("5908c91e0a975a7b6e97bc24"),
"description" : "Since the first edition until 1992 ",
"publisher" : ObjectId("578bb0f0e403c10f2f490d8f"),
"title" : "National Geographic",
"imgs" : []
}
/* 3 */
{
"_id" : ObjectId("5907b0d00a975a7b6e97bc22"),
"description" : "3 months old kitten",
"publisher" : ObjectId("578bb0f0e403c10f2f490d8f"),
"title" : "Bino",
"imgs" : [
{
"url" : "http://localhost:8088/ad/578bb0f0e403c10f2f490d8f-31ec293361cf7d318a189b7678841935a404249718d5f6a4d10ee70017aa600a.png"
},
{
"url" : "http://localhost:8088/ad/578bb0f0e403c10f2f490d8f-1884915f5d94c6566a9bc329b673b5b525ff09329500c596a3209356006de0e5.png"
},
{
"url" : "http://localhost:8088/ad/578bb0f0e403c10f2f490d8f-a1f7b7bbb9cbb89dd7dd61e6aa26a6fde2357f01296ebe47956205858a4f55a3.png"
},
{
"url" : "http://localhost:8088/ad/578bb0f0e403c10f2f490d8f-e86c731f23635e86ffb19333bdefba9761a6a2b8cbbc1ffe198acd6f7215e8db.png"
}
]
}
我想获取图像 URL 并将它们显示在标签中。 所以我用刀片模板写了这个,
@foreach ($db as $item)
<div class="divTableRow">
<div class="divTableCell"> </div>
<div class="divTableCell"> </div>
<div class="divTableCell">{{$item ->title}}</div>
<div class="divTableCell">{{$item->description}}</div>
<div class="divTableCell"> </div>
<div class="divTableCell">
@foreach ($item->imgs as $imgs)
<img src="{{$imgs->url}}"/>
@endforeach
</div>
<div class="divTableCell"> </div>
</div>
@endforeach
但这给了我两个错误。
我该如何解决这个问题?
【问题讨论】:
-
查看 laravel 日志。您遇到什么错误?这段代码我觉得不错。
-
嗨 piotr,我上传了带有错误消息的图片。
-
我认为您的
$item或$db不是对象。使用常规的<?php foreach() ?>语法临时更改刀片@foreach并使用try{} catch{}块查找不是对象的项目。 -
我从我的路由文件
Route::get('/mongo', function () { $db = Mongotest::all(); return view('mongo',compact('db')); });中得到$db,当我将它放入print_r($db);时,它会给出很好的输出。 -
如我所说。在
foreach $item->imgs as $img循环中使用try catch。这将向您显示无效的$img对象。并运行php artisan cache:clear以确保旧错误没有问题
标签: php arrays mongodb laravel-5.4 blade