【问题标题】:Laracasts Presenter on a Paginated Instance分页实例上的 Laracasts Presenter
【发布时间】:2015-03-09 16:50:48
【问题描述】:

在我将其设为分页器实例之前,我的演示者可以完美运行。我意识到它不再是模型对象,因此无法调用 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 )。我在提取缓存的集合后手动创建它。当我只返回集合时,演示者工作得很好。但是,我不想手动缓存每个分页页面,因为我在创建每个新主题时都附加了一条新记录,因此我不必清除和重新缓存以将该处理保存在我的服务器上。

我可以在分页器实例上执行 getCollection(),但这只是返回一个通用集合,而不是论坛主题集合(模型),因此它不知道哪个演示者附加到集合。

当我执行 dd() 时,我得到一个分页器对象: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)

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

【问题讨论】:

    标签: laravel pagination


    【解决方案1】:

    看来我可能已经想通了 :) 我的代码现在看起来像这样。在我的控制器中,我有:

    $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 等)并且您的演示者仍然可以正常工作:) 并且您的缓存保持不变。希望这对其他人有所帮助!

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 2014-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多