【发布时间】:2017-01-06 13:15:09
【问题描述】:
我的分页有一些问题,我正在尝试在我正在制作的论坛部分对我的 cmets 进行分页。它适用于我的“类别”和“线程”,但对于 cmets,我似乎根本无法让我的分页工作。
这是适用于线程的代码:
public function category($id){
$category = ForumCategory::find($id);
if ($category == null){
return Redirect::route('forum')->with('fail', "That category doesn't exist.");
}
$threads = $category->threads()->paginate(10);
return View::make('forum.category')->with('category', $category)->with('threads', $threads);
}
下面是对 cmets 不起作用的代码:
public function thread($id){
$thread = ForumThread::find($id);
if ($thread == null){
return Redirect::route('forum')->with('fail', "That thread doesn't exist.");
}
$author = $thread->author()->paginate(5)->first();
return View::make('forum.thread')->with('thread', $thread)->with('author', $author);
}
【问题讨论】:
-
你用了
paginate函数后为什么还要调用->first()? -
如果我不这样做,我会收到此错误:未定义的属性:Illuminate\Pagination\Paginator::$avatar (View: D:\websites\laravel_nfgm8\app\views\forum\thread.blade .php)不幸的是,我在 laravel 4.2 的论坛中关注的 youtube 教程系列。一开始已被删除,我现在得到的唯一参考是从那时起我的文件中,所以我不确定还有什么可以让它工作:)
-
我不明白您为什么要对某些 cmets 进行分页,但该关系称为
author。关系如何? -
你能在你的视图中显示你用来迭代这个分页器的代码吗?我认为那里出了点问题。 (结合控制器代码中的
first()) -
我猜你想的关系是category、threads和cmets的数据库表之间的链接?就是这样:
<?php class ForumCategory extends Eloquent{ protected $table = 'forum_categories'; public function threads(){ return $this->hasMany('ForumThread', 'category_id'); } public function comments(){ return $this->hasMany('ForumComment', 'category_id'); } protected $fillable = array('title', 'text', 'created_by', 'avatar'); }
标签: php laravel comments forum pagination