【发布时间】: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