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