我用查询构建器创建了一个返回多行的查询,当我尝试使用foreach循环打印记录时,出现此错误:
"试图获得非对象的属性"
Controller:
$productPhotos = DB::table('productsphotos')->where('ProductId', $id);
View:
@foreach ($productPhotos as $item)
<li><img src="{{ $item->PhotoThumb }}" /></li>
@endforeach
答案 0 :(得分:1)
更改
$productPhotos = DB::table('productsphotos')->where('ProductId', $id);
到
$productPhotos = DB::table('productsphotos')->where('ProductId', $id)->get();
答案 1 :(得分:0)
$productPhotos = DB::table('productsphotos')->where('ProductId', $id)->get();
此查询将为您提供对象数组,就像您可以使用数组一样
$productPhotos = DB::table('productsphotos')->where('ProductId', $id)->get()->toArray();