Laracasts Presenter在一个Paginated实例上

时间:2015-03-09 16:50:48

标签: laravel pagination

我的演示者工作得很好,直到我把它作为一个paginator实例。我意识到它不再是模型对象,因此无法调用present()。有没有解决方法,所以你仍然可以在分页实例上使用你的演示者?

这是我的代码: 在我的控制器中我有这个:

$topics = $this->topic->getPaginatedTopics( $id );

在我的主题回购中,我有这个:

/*************************
 * QUERIES *
*************************/
/**
 * Get topics under category
 * @param id       Category ID
 *
 * @return Eloquent Object
 */
public function getByCategory( $id )
{
        if( ! $this->cache->has( $this->getTopicCacheName( $id ) ) )
    {
        $data = $this->getCategoryWithTopics( $id );

        return $this->cache->getRemember( $this->getTopicCacheName( $id ), $data->topics, 20 );
    }

    return $this->cache->get( $this->getTopicCacheName( $id ) );
}

/**
 * Get paginated topics
 * @param id       Category ID
 *
 * @return Exemplary Object
 */
public function getPaginatedTopics( $id )
{
    $topics = $this->getByCategory( $id );

    return $this->paginator->make( $topics->toArray(), count($topics), $this->topicsPerPage );
}

这会返回一个Eloquent集合。我有缓存涉及所以我不在我的查询中调用paginate(20)。我拉完缓存的集合后手工制作它。当我回到收藏品时,主持人工作得很好。但是,我不想手动缓存每个分页页面,因为我在创建的每个新主题后附加了一条新记录,因此我不必清除并重新缓存以在我的服务器上保存该处理。

我可以在paginator实例上执行getCollection(),但这只返回一个通用集合,而不是一个forumtopic集合(模型),所以它不知道哪个演示者附加到集合。

当我执行dd()时,我得到一个paginator对象:object(Illuminate \ Pagination \ Paginator)[606]当我通过 - > toArray()把它变成一个数组时,我得到了这个:

array (size=7)
  'total' => int 7
  'per_page' => int 25
  'current_page' => int 1
  'last_page' => int 1
  'from' => int 1
  'to' => int 7
  'data' => 
    array (size=7)
      0 => 
        array (size=7)
          'id' => int 65
          'title' => string 'This should append to the top' (length=29)
          'category_id' => int 1
          'user_id' => int 3
          'sticky' => int 0
          'locked' => int 0
          'created_at' => string '2015-03-06 21:53:55' (length=19)
      1 => 
        array (size=7)
          'id' => int 64
          'title' => string 'Appendage should work' (length=21)
          'category_id' => int 1
          'user_id' => int 3
          'sticky' => int 0
          'locked' => int 0
          'created_at' => string '2015-03-06 21:51:42' (length=19)
      2 => 
        array (size=7)
          'id' => int 63
          'title' => string 'COME ON!' (length=8)
          'category_id' => int 1
          'user_id' => int 3
          'sticky' => int 0
          'locked' => int 0
          'created_at' => string '2015-03-06 21:36:12' (length=19)
      3 => 
        array (size=7)
          'id' => int 51
          'title' => string 'This would should work like a charm!' (length=36)
          'category_id' => int 1
          'user_id' => int 3
          'sticky' => int 0
          'locked' => int 0
          'created_at' => string '2015-03-06 21:26:28' (length=19)
      4 => 
        array (size=7)
          'id' => int 50
          'title' => string 'Testing cache clear' (length=19)
          'category_id' => int 1
          'user_id' => int 3
          'sticky' => int 0
          'locked' => int 0
          'created_at' => string '2015-03-06 21:20:36' (length=19)
      5 => 
        array (size=7)
          'id' => int 46
          'title' => string 'Appending to cache?' (length=19)
          'category_id' => int 1
          'user_id' => int 3
          'sticky' => int 0
          'locked' => int 0
          'created_at' => string '2015-03-06 21:17:49' (length=19)
      6 => 
        array (size=7)
          'id' => int 33
          'title' => string 'Testing presenter' (length=17)
          'category_id' => int 1
          'user_id' => int 3
          'sticky' => int 0
          'locked' => int 0
          'created_at' => string '2015-03-06 19:06:48' (length=19)

只是为了告诉你我得到了什么。谢谢你的帮助! :)

1 个答案:

答案 0 :(得分:0)

似乎我可能已经弄明白了:)我的代码现在看起来像这样。在我的控制器中,我有:

$this->topic->getPaginatedTopics( $id, $this->input->get('page') );

在我的回购中,我现在有:

/*************************
 * QUERIES *
*************************/
/**
 * Get topics under category
 * @param id       Category ID
 *
 * @return Eloquent Object
 */
public function getByCategory( $id )
{
        if( ! $this->cache->has( $this->getTopicCacheName( $id ) ) )
    {
        $data = $this->getCategoryWithTopics( $id );

        return $this->cache->getRemember( $this->getTopicCacheName( $id ), $data->topics, 20 );
    }

    return $this->cache->get( $this->getTopicCacheName( $id ) );
}

/**
 * Get paginated topics
 * @param id       Category ID
 *
 * @return Exemplary Object
 */
public function getPaginatedTopics( $id, $page = 1 )
{
    $topics = $this->getByCategory( $id )->sortByDesc(function($topic)
    {
        return $topic->created_at;
    });

    $offset = (($page - 1) * $this->topicsPerPage);

    return $this->paginator->make(
        $topics->slice($offset, $this->topicsPerPage, true)->all(),
        $topics->count(),
        $this->topicsPerPage
    );
}

然后你的视图看起来就像你在你的记录中迭代一样,可以调用他们的属性,如$ topic-> title等等,你的演示者仍然可以正常工作:)并且你的缓存保持原样。希望这有助于其他人!