我正在尝试从我的表中获取数据并在我的视图中显示它并对这些数据进行分页。我收到了这个问题,无法找到任何解决方案。我在之前的项目中做了完全相同的事情并且效果很好。
这是我的控制器
foreach ($userDetails->wanttoread as $key => $bookid) {
$bookDetails = DB::table('books')->where('_id', $bookid)->paginate(10);
array_push($wanttoreadArr, $bookDetails);
}
$data['wanttoread'] = $wanttoreadArr;
return view('frontend.user.userShelve')->with($data);
这是我的观点
<tbody>
@foreach($wanttoread as $book)
<tr>
<td><img src="{{ URL::to($book->thumbnailLink) }}" class="img-thumbnail img-responsive"></td>
</tr>
@endforeach
</tbody>
答案 0 :(得分:1)
您可以尝试这样做(保持控制器代码不变)
<tbody>
@foreach($wanttoread as $row)
@foreach($row->items() as $book)
<tr>
<td><img src="{{ URL::to($book['thumbnailLink']) }}" class="img-thumbnail img-responsive"></td>
</tr>
@endforeach
@endforeach
</tbody>
答案 1 :(得分:1)
您可以通过两种方式解决此问题
1个解决方案更换刀片
<tbody>
@foreach($wanttoread as $books)
@foreach($books as $book)
<tr>
<td><img src="{{ URL::to($book["thumbnailLink"]) }}" class="img-thumbnail img-responsive"></td>
</tr>
@endforeach
@endforeach
</tbody>
2解决方案chage控制器逻辑(获得分页结果和所有时间相同计数项的最佳解决方案)
$bookDetails = DB::table('books')->whereIn('_id', $userDetails->wanttoread)->paginate(10);
$data['wanttoread'] = $wanttoreadArr;
return view('frontend.user.userShelve')->with($data);
答案 2 :(得分:0)
假设您正在获取LengthAwarePaginator,您需要获取当前正在分页的项目,因此您可以执行以下操作:
<tbody>
@foreach($wanttoread->items() as $book)
<tr>
<td><img src="{{ URL::to($book->thumbnailLink) }}" class="img-thumbnail img-responsive">
</td>
</tr>
@endforeach
items()函数将返回数据集。您可以在官方API docs上找到更多信息。