本文整理汇总了PHP中Illuminate\Pagination\Paginator类的典型用法代码示例。如果您正苦于以下问题:PHP Paginator类的具体用法?PHP Paginator怎么用?PHP Paginator使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Paginator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: paginate
/**
* Paginates the Elasticsearch results.
*
* @param int $perPage
* @return mixed
*/
public function paginate($perPage = 15)
{
$paginator = new Paginator($this->items, $perPage);
$start = ($paginator->currentPage() - 1) * $perPage;
$sliced = array_slice($this->items, $start, $perPage);
return new Paginator($sliced, $perPage);
}
示例2: timeLine
public function timeLine()
{
$tags = Auth::user()->tags()->get();
$posts = collect([]);
foreach ($tags as $tag) {
foreach ($tag->post()->get() as $post) {
if (!Auth::user()->type) {
if (!$post->private) {
$posts->push($post);
} else {
if ($post->user_id == Auth::id()) {
$posts->push($post);
}
}
} else {
$posts->push($post);
}
}
}
$posts = new Paginator($posts->unique('id'), 10);
if (strpos(redirect()->back()->getTargetUrl(), 'login') === false) {
return view('welcome', compact('posts', 'tags'));
} else {
return view('welcome', compact('posts', 'tags'))->with('message', 'Welcome ' . Auth::user()->fullName());
}
}
示例3: paginatedCollection
public function paginatedCollection(Paginator $paginator, $transformer = null, $resourceKey = null)
{
$paginator->appends(\Request::query());
$resource = new Collection($paginator->getCollection(), $this->getTransformer($transformer), $resourceKey);
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return $this->manager->createData($resource)->toArray();
}
示例4: withPaginator
/**
* Respond with a paginator, and a transformer.
*
* @param Paginator $paginator
* @param callable|\League\Fractal\TransformerAbstract $transformer
* @param string $resourceKey
* @param array $meta
* @return \Illuminate\Http\Response
*/
public function withPaginator(Paginator $paginator, $transformer, $resourceKey = null, $meta = [])
{
$resource = new Collection($paginator->getCollection(), $transformer, $resourceKey);
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
foreach ($meta as $metaKey => $metaValue) {
$resource->setMetaValue($metaKey, $metaValue);
}
$rootScope = $this->manager->createData($resource);
return $this->withArray($rootScope->toArray());
}
示例5: answerOkByPager
static function answerOkByPager(Paginator $paginator)
{
if ($paginator->getLastPage() == $paginator->getCurrentPage()) {
$next_page = null;
} else {
$next_page = $paginator->getCurrentPage();
}
$next_page_url = $next_page ? \Request::url() . '?page=' . $next_page : null;
return parent::json(['meta' => ['code' => 200], 'pagination' => ['next_url' => $next_page_url, 'next_page' => $next_page], 'data' => $paginator->getCollection()->toArray()], 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
示例6: paginator
public function paginator(\Illuminate\Pagination\Paginator &$paginator)
{
$items = [];
foreach ($this as $index => $item) {
if ($index < $paginator->getFrom() - 1 || $index > $paginator->getTo() - 1) {
continue;
}
$items[] = $item;
}
$paginator->setItems($items);
return $paginator;
}
示例7: paginate
/**
* Return the paginated dataset of the underlying database.
*
* @param int $perPage
* @param array $columns
* @param string $pageName
* @param int $page
*
* @return \Illuminate\Pagination\Paginator
*/
public function paginate($perPage, $columns, $pageName, $page)
{
$total = count($this->db);
$page = $page ?: Paginator::resolveCurrentPage($pageName);
$results = array_slice($this->get(), ($page - 1) * $perPage, $perPage);
return new LengthAwarePaginator($results, $total, $perPage, $page, ['path' => Paginator::resolveCurrentPath(), 'pageName' => $pageName]);
}
示例8: readLog
public static function readLog($deviceId, $page)
{
Paginator::currentPageResolver(function () use($page) {
return $page;
});
return LocationLog::where('device_id', $deviceId)->orderBy('location_time', 'DESC')->simplePaginate(config('custom.item_per_page'))->all();
}
示例9: response
/**
* @param $results
* @param $with
* @param $paginated
* @param Searchable|null $model
* @return array|LengthAwarePaginator
*/
protected function response($results, $with, $paginated, Searchable $model = null)
{
$collection = $this->asModels($results['hits']['hits'], $model);
/*
* if we also want to lazy load relations, we'll create a collection and load them,
* pass them on to the paginator if needed
* heads up: i believe nested documents will always be loaded,
* so developer should only pass with relations that aren't being indexed by Elasticsearch
*/
if ($with) {
$model->unguard();
$collection = $model->newCollection($collection);
$model->reguard();
$collection->load($with);
}
if ($paginated) {
/*
* if we lazy loaded some relations, we need to get back an array to paginate.
* not an optimal way of doing this, but i believe there isn't a better way at this point,
* since the paginator only takes an array.
*/
$collection = is_array($collection) ? $collection : $collection->all();
$path = Paginator::resolveCurrentPath();
//for some reason things do not work when passing in the options as an regular array
$results = new LengthAwarePaginator($collection, $results['hits']['total'], $paginated);
$results->setPath($path);
//only need transform into a collection when we didn't lazyload relations
} elseif (is_array($collection)) {
$results = $model->newCollection($collection);
} else {
$results = $collection;
}
return $results;
}
示例10: paginateCollection
/**
* Paginates a collection. For simple pagination, one can override this function
*
* a little help from http://laravelsnippets.com/snippets/custom-data-pagination
*
* @param Collection $data
* @param int $perPage
* @param Request $request
* @param null $page
*
* @return LengthAwarePaginator
*/
public function paginateCollection(Collection $data, $perPage, Request $request, $page = null)
{
$pg = $request->get('page');
$page = $page ? (int) $page * 1 : (isset($pg) ? (int) $request->get('page') * 1 : 1);
$offset = $page * $perPage - $perPage;
return new LengthAwarePaginator($data->splice($offset, $perPage), $data->count(), $perPage, Paginator::resolveCurrentPage(), ['path' => Paginator::resolveCurrentPath()]);
}
示例11: handle
/**
* Set the current page based on the page route parameter before the route's action is executed.
*
* @return \Illuminate\Http\Request
*/
public function handle($request, Closure $next)
{
Paginator::currentPageResolver(function () {
return app('paginateroute')->currentPage();
});
return $next($request);
}
示例12: feed
/**
* Список новостей
* @param Request $request
* @param null $alias
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function feed(Request $request, $alias = null)
{
$page = Paginator::resolveCurrentPage('page');
$per_page = config('paginator.per_page');
$category = Category::where('alias', $alias)->first();
$order_by = $request->input('order_by');
$articles = Article::whereRaw('1=1');
switch ($order_by) {
case 'name_desc':
$articles->orderBy('name', 'desc');
break;
case 'name_asc':
$articles->orderBy('name', 'asc');
break;
case 'date_desc':
$articles->orderBy('created_at', 'desc');
break;
case 'date_asc':
$articles->orderBy('created_at', 'asc');
break;
default:
$articles->orderBy('created_at', 'desc');
break;
}
if (is_null($category)) {
$articles = $articles->paginate($per_page);
} else {
$articles = $articles->where('category_id', $category->id)->paginate($per_page);
}
if ($order_by) {
$articles->appends('order_by', $order_by);
}
return view('pages/feed', ['articles' => $articles, 'category' => $category, 'page' => $page, 'order_by' => $order_by]);
}
示例13: validateQuery
public function validateQuery(Request $request)
{
$modelClass = $this->model;
$queryBuilder = new $modelClass();
if (count($parameters = $request->all()) > 0) {
$handledRequestParameters = $this->handleRequestParameters($parameters);
if (isset($handledRequestParameters['queries'])) {
foreach ($handledRequestParameters['queries'] as $query) {
$queryBuilder = $this->handleQuery($queryBuilder, $query);
}
}
if (isset($handledRequestParameters['take'])) {
$queryBuilder = $queryBuilder->take($handledRequestParameters['take']);
} else {
if (isset($handledRequestParameters['pagination'])) {
$currentPage = $handledRequestParameters['pagination']['page'];
Paginator::currentPageResolver(function () use($currentPage) {
return $currentPage;
});
return $queryBuilder = $queryBuilder->paginate($handledRequestParameters['pagination']['paginate'])->setPath($this->getUrlParameters($request));
}
}
}
return $queryBuilder->get();
}
示例14: getPaginatedEntries
/**
* Returns the entries for the current page for this view.
*
* @return \Illuminate\Pagination\LengthAwarePaginator paginator containing the entries for the page, sorted/ordered or not.
*/
public function getPaginatedEntries()
{
// Gets all the entries, sensitive to whether we're sorting for this request.
$allEntries = $this->getEntriesSortable();
$page = Paginator::resolveCurrentPage('page');
// Returns the number of entries perpage, defined by Model#getPerPage
$perPage = $allEntries->first()->getPerPage();
// If the page number is beyond the number of pages, get it back to the last page.
while (($page - 1) * $perPage > count($allEntries)) {
$page -= 1;
}
// Return the subset of the entries for this page
$entriesForPage = $allEntries->splice(($page - 1) * $perPage, $perPage);
// Return the paginator for this subset.
$entriesPaginator = new LengthAwarePaginator($entriesForPage, $this->getEntries()->first()->toBase()->getCountForPagination(), $perPage, $page, ['path' => Paginator::resolveCurrentPath(), 'pageName' => 'page']);
// If we're ordering, append that to the links
if ($this->getSortOrder()) {
$entriesPaginator->appends(['order' => Request::get('order')]);
}
// If we're sorting, append that to the links
if ($this->isSorting()) {
$entriesPaginator->appends(['sort' => $this->getSortKey()]);
}
return $entriesPaginator;
}
示例15: AllContents
/**
* 取得未删除的信息
*
* @return array
* @todo 数据量多时,查找属于指定分类,推荐位,标签三个的文章时使用redis集合交集处理,避免查询消耗。
*/
public function AllContents($search = [])
{
$prefix = \DB::getTablePrefix();
$currentQuery = $this->select(['article_main.*', 'users.name'])->leftJoin('users', 'article_main.user_id', '=', 'users.id')->leftJoin('article_classify_relation', 'article_main.id', '=', 'article_classify_relation.article_id')->leftJoin('article_classify', 'article_classify_relation.classify_id', '=', 'article_classify.id')->leftJoin('article_position_relation', 'article_main.id', '=', 'article_position_relation.article_id')->leftJoin('article_tag_relation', 'article_main.id', '=', 'article_tag_relation.article_id')->orderBy('article_main.id', 'desc')->where('article_main.is_delete', self::IS_DELETE_NO)->groupBy('article_main.id')->distinct();
if (isset($search['keyword']) && !empty($search['keyword'])) {
$currentQuery->where('article_main.title', 'like', "%{$search['keyword']}%");
}
if (isset($search['username']) && !empty($search['username'])) {
$currentQuery->where('article_main.user_id', $search['username']);
}
if (isset($search['classify']) && !empty($search['classify'])) {
$currentQuery->where('article_classify_relation.classify_id', $search['classify']);
}
if (isset($search['position']) && !empty($search['position'])) {
$currentQuery->where('article_position_relation.position_id', $search['position']);
}
if (isset($search['tag']) && !empty($search['tag'])) {
$currentQuery->where('article_tag_relation.tag_id', $search['tag']);
}
if (isset($search['timeFrom'], $search['timeTo']) and !empty($search['timeFrom']) and !empty($search['timeTo'])) {
$search['timeFrom'] = strtotime($search['timeFrom']);
$search['timeTo'] = strtotime($search['timeTo']);
$currentQuery->whereBetween('article_main.write_time', [$search['timeFrom'], $search['timeTo']]);
}
$total = count($currentQuery->get()->all());
$currentQuery->forPage($page = Paginator::resolveCurrentPage(), $perPage = self::PAGE_NUMS);
$result = $currentQuery->get()->all();
return new LengthAwarePaginator($result, $total, $perPage, $page, ['path' => Paginator::resolveCurrentPath()]);
}