Laravel 5查询生成器记录集数组

时间:2015-02-13 04:50:14

标签: php arrays laravel-5

我用查询构建器创建了一个返回多行的查询,当我尝试使用foreach循环打印记录时,出现此错误:

"试图获得非对象的属性"

Controller:
$productPhotos = DB::table('productsphotos')->where('ProductId', $id);

View:
@foreach ($productPhotos as $item)
  <li><img src="{{ $item->PhotoThumb }}" /></li>
@endforeach

2 个答案:

答案 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();