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

问题描述 投票:1回答:2

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

“试图获得非对象的属性”

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

View:
@foreach ($productPhotos as $item)
  <li><img src="{{ $item->PhotoThumb }}" /></li>
@endforeach
php arrays laravel-5
2个回答
1
投票

更改

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

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

0
投票
$productPhotos = DB::table('productsphotos')->where('ProductId', $id)->get();

此查询将为您提供对象数组,就像您想要使用的数组一样

$productPhotos = DB::table('productsphotos')->where('ProductId', $id)->get()->toArray();
© www.soinside.com 2019 - 2024. All rights reserved.