【问题标题】:Laravel 4.2 Pagination not workingLaravel 4.2分页不起作用
【发布时间】: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);
}

下面是对 cme​​ts 不起作用的代码:

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


【解决方案1】:

好的,所以我找到了一个修复它,它对我有用,唯一的事情是我现在需要找到一种方法来制作它,以便线程的主要消息(第一篇帖子是数据库上 forum_threads 的一部分)一起工作然而,随着 cmets 的分页,现在 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()->first();
    $comment = $thread->comments()->paginate(5);

    return View::make('forum.thread')->with('thread', $thread)->with('author', $author)->with('comment', $comment);
}

【讨论】:

    猜你喜欢
    • 2014-08-15
    • 1970-01-01
    • 2014-10-19
    • 2018-06-25
    • 2016-11-28
    • 2016-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-21
    相关资源
    最近更新 更多