我想實現在laravel分頁使用分頁與laravel雄辯的查找方法一起,得到了以下錯誤如何laravel 5.4
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$name
這裏是我的控制器功能
public function showTags($id)
{
$tag = Tag::find($id)->paginate(5);
// when lazy loading
$tag->load(['posts' => function ($q) {
$q->orderBy('id', 'desc');
}]);
return view('blog.showtags')->withTag($tag);
}
這裏是標籤模型
class Tag extends Model
{
public function posts()
{
return $this->belongsToMany('App\Post');
}
}
標籤和帖子模型有belongsToMany關係,所以有很多帖子u在特定標籤之前,我的目標是按照帖子的降序重複所有帖子,並在該頁面中實現分頁。
這裏是代碼showtags查看
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Tags</th>
</tr>
</thead>
<tbody>
<?php $count = 1; ?>
@foreach($tag->posts as $post)
<tr>
<th>{{ $count++ }}</th>
<th>{{ $post->title }}</th>
<th>@foreach($post->tags as $tag)
<span class="label label-default">{{ $tag->name }}</span>
@endforeach
</th>
</tr>
@endforeach
</tbody>
</table>
//Here is the code i used for pagination in view
<div class="text-center">
{!! $tag->posts->links() !!}
</div>
如果有人知道如何做到這一點請回復。提前致謝。
不知道如果我失去了一些東西,但'的鏈接find'只得到單行,這樣你就可以」 t分頁。你想做什麼? – apokryfos
@apokryfos我知道找到只給一行,但你可以看到在我看來,我迭代每個帖子在同一個標簽下,因爲標籤已經屬於與帖子的許多關係。我的目標是通過分頁來組織所有帖子。那麼有什麼辦法可以做到這一點 –
所以你可能會做'$ tag-> posts() - > paginate(5)'或類似的東西。 – apokryfos