Skip to content

Commit

Permalink
Merge branch 'develop' into 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Mar 6, 2022
2 parents 562fe09 + 2b5c7ab commit 3737581
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 49 deletions.
50 changes: 2 additions & 48 deletions src/Database/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
class Builder extends BuilderModel
{
use \October\Rain\Database\Concerns\HasNicerPagination;

/**
* Get an array with the values of a given column.
*
Expand Down Expand Up @@ -102,30 +104,6 @@ protected function searchWhereInternal($term, $columns, $mode, $boolean)
return $this;
}

/**
* paginateAtPage paginates by passing the page number directly
*
* @param int $perPage
* @param int $currentPage
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function paginateAtPage($perPage, $currentPage)
{
return $this->paginate($perPage, ['*'], 'page', $currentPage);
}

/**
* paginateCustom paginates using a custom page name.
*
* @param int $perPage
* @param string $pageName
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function paginateCustom($perPage, $pageName)
{
return $this->paginate($perPage, ['*'], $pageName);
}

/**
* paginate the given query.
*
Expand Down Expand Up @@ -166,30 +144,6 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',
]);
}

/**
* simplePaginateAtPage simply paginates by passing the page number directly
*
* @param int $perPage
* @param int $currentPage
* @return \Illuminate\Contracts\Pagination\Paginator
*/
public function simplePaginateAtPage($perPage, $currentPage)
{
return $this->simplePaginate($perPage, ['*'], 'page', $currentPage);
}

/**
* simplePaginateCustom simply paginates using a custom page name.
*
* @param int $perPage
* @param string $pageName
* @return \Illuminate\Contracts\Pagination\Paginator
*/
public function simplePaginateCustom($perPage, $pageName)
{
return $this->simplePaginate($perPage, ['*'], $pageName);
}

/**
* simplePaginate the given query into a simple paginator.
*
Expand Down
55 changes: 55 additions & 0 deletions src/Database/Concerns/HasNicerPagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php namespace October\Rain\Database\Concerns;

/**
* HasNicerPagination for a query builder
*/
trait HasNicerPagination
{
/**
* paginateAtPage paginates by passing the page number directly
*
* @param int $perPage
* @param int $currentPage
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function paginateAtPage($perPage, $currentPage)
{
return $this->paginate($perPage, ['*'], 'page', $currentPage);
}

/**
* paginateCustom paginates using a custom page name.
*
* @param int $perPage
* @param string $pageName
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function paginateCustom($perPage, $pageName)
{
return $this->paginate($perPage, ['*'], $pageName);
}

/**
* simplePaginateAtPage simply paginates by passing the page number directly
*
* @param int $perPage
* @param int $currentPage
* @return \Illuminate\Contracts\Pagination\Paginator
*/
public function simplePaginateAtPage($perPage, $currentPage)
{
return $this->simplePaginate($perPage, ['*'], 'page', $currentPage);
}

/**
* simplePaginateCustom simply paginates using a custom page name.
*
* @param int $perPage
* @param string $pageName
* @return \Illuminate\Contracts\Pagination\Paginator
*/
public function simplePaginateCustom($perPage, $pageName)
{
return $this->simplePaginate($perPage, ['*'], $pageName);
}
}
47 changes: 46 additions & 1 deletion src/Database/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class BelongsToMany extends BelongsToManyBase
{
use DeferOneOrMany;
use DefinedConstraints;
use \October\Rain\Database\Concerns\HasNicerPagination;

/**
* @var bool countMode sets this relation object is a 'count' helper
Expand Down Expand Up @@ -252,8 +253,20 @@ public function remove(Model $model, $sessionKey = null)
* @param string $pageName
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function paginate($perPage = 15, $currentPage = null, $columns = ['*'], $pageName = 'page')
public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $currentPage = null)
{
// Legacy signature support
// paginate($perPage, $currentPage, $columns, $pageName)
if (!is_array($columns)) {
$_currentPage = $columns;
$_columns = $pageName;
$_pageName = $currentPage;

$columns = is_array($_columns) ? $_columns : ['*'];
$pageName = $_pageName !== null ? $_pageName : 'page';
$currentPage = is_array($_currentPage) ? null : $_currentPage;
}

$this->query->addSelect($this->shouldSelect($columns));

$paginator = $this->query->paginate($perPage, $currentPage, $columns);
Expand All @@ -263,6 +276,38 @@ public function paginate($perPage = 15, $currentPage = null, $columns = ['*'], $
return $paginator;
}

/**
* simplePaginate using a simple paginator.
*
* @param int|null $perPage
* @param array $columns
* @param string $pageName
* @param int|null $page
* @return \Illuminate\Contracts\Pagination\Paginator
*/
public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $currentPage = null)
{
// Legacy signature support
// paginate($perPage, $currentPage, $columns, $pageName)
if (!is_array($columns)) {
$_currentPage = $columns;
$_columns = $pageName;
$_pageName = $currentPage;

$columns = is_array($_columns) ? $_columns : ['*'];
$pageName = $_pageName !== null ? $_pageName : 'page';
$currentPage = is_array($_currentPage) ? null : $_currentPage;
}

$this->query->addSelect($this->shouldSelect($columns));

$paginator = $this->query->simplePaginate($perPage, $currentPage, $columns);

$this->hydratePivotRelation($paginator->items());

return $paginator;
}

/**
* newPivot creates a new pivot model instance
*
Expand Down

0 comments on commit 3737581

Please sign in to comment.