如果我使用
$categories= Category::orderby('id', 'desc')->paginate(3);
它有效,但如果我使用
$categories= Category::with('subcategories')->orderby('id', 'desc')->paginate(3);
paginate不起作用!如何解决?
更新:
<tbody>
@foreach ($categories as $category)
@foreach($category->subcategories as $sub)
<tr>
<td class="text-center">{{ $sub->id }}</td>
<td class="text-center">{{ $sub->title }}</td>
<td class="text-center">{{ $sub->category->title }}</td>
</tr>
@endforeach
@endforeach
</tbody>
答案 0 :(得分:3)
你需要从关系的另一面来解决这个问题。
查找所有子类别及其父类别,按ID降序排序,然后每页分页三个。
$subcategories = Subcategory::with('category')
->orderBy('id', 'desc')
->paginate(3);
<tbody>
@foreach($subcategories as $subcategory)
<tr>
<td class="text-center">{{ $subcategory->id }}</td>
<td class="text-center">{{ $subcategory->title }}</td>
<td class="text-center">{{ $subcategory->category->title }}</td>
</tr>
@endforeach
</tbody>