/** * Get results by page * if data has conditions, it must be queried before call this method * edited from source : http://culttt.com/2014/02/24/working-pagination-laravel-4/ * @param string $key : key cache for this list, not inlude page * @param int $page * @param int $perpage * @return Paginator */ public function scopeByPage($query, $key, $cacheTime, $perpage = 10, $page = null) { $page = $page !== null ? $page : Input::get('p'); $page = intval($page) < 1 ? 1 : $page; $key = $key . $page . "_" . $perpage; $items = Cache::remember($key, $cacheTime, function () use($perpage, $page, $query) { return $query->skip($perpage * ($page - 1))->take($perpage)->get(); }); $totalItems = $query->count(); $data = $items->all(); $retval = Paginator::make($data, $totalItems, $perpage); return $retval; }