Skip to content

Commit 78171c8

Browse files
author
Nicholas Dykhuizen
committed
Upgrading pagination
1 parent d7aa5a1 commit 78171c8

File tree

5 files changed

+46
-24
lines changed

5 files changed

+46
-24
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Changelog
2+
3+
All notable changes to `dykhuizen/laravel-datatables` will be documented in this file
4+
5+
## 2.0.0 - 2020-11-22
6+
7+
- Added `simplePaginateable($forcePagination = true)`
8+
- Altered `paginateable` to have `$forcePagination` as a parameter
9+
- Changed `simplePaginateable` and `paginateable` to take a $forcePagination parameter. If this value is false, the query will default to a `->get()` call. If true, the query will default to its respective pagination function.
10+
- Added `$maxPerPage` to `Paginateable` to prevent dos-like queries.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This single trait gives you access the following traits:
1919
* Searchable - Applies a search query for selected columns
2020
* Filterable - Applies a list of filters to the query based on columns selected
2121
* Paginateable - Either runs the `$eloquent->get()` or `$eloquent->paginate()` methods based on the request
22+
* SimplePaginateable - Either runs the `$eloquent->get()` or `$eloquent->simplePaginate()` methods based on the request
2223
* Selectable - Selects the response data to be returned
2324

2425

@@ -139,13 +140,13 @@ public function addressFilterable($query, $filters)
139140
}
140141
```
141142

142-
## Paginateable
143+
## Paginateable and SimplePaginateable
143144
The paginateable columns default to `page` and `per_page` just like Laravel's built in pagination
144145
<br>
145146
These keys can be overwritten on a per model instance by setting the `$paginateablePageKey` and `$paginateablePerPageKey` variables.
146147
<br>
147148
If these keys exist, results will be paginated in the response.
148-
If these keys to not exist, results will be returned via the `get()` method
149+
If these keys do not exist, the $forcePagination function param will be checked. If true, results will be paginated according to Laravel defaults. If false, results will be retrieved via the `get()` method.
149150
<br>
150151
<br>
151152
Ex:

src/Datatable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Dykhuizen\Datatable;
44

5-
use Gofish\Datatable\Traits\Searchable;
65
use Illuminate\Database\Eloquent\Builder;
76
use Illuminate\Database\Eloquent\Model;
87
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@@ -17,7 +16,8 @@
1716
* @method static static|self|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder searchable()
1817
* @method static static|self|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder sortable()
1918
* @method static static|self|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder filterable()
20-
* @method static static|self|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder paginateable()
19+
* @method static static|self|\Illuminate\Database\Eloquent\Collection paginateable($forcePagination = true)
20+
* @method static static|self|\Illuminate\Database\Eloquent\Collection simplePaginateable($forcePagination = true)
2121
*/
2222
trait Datatable {
2323

@@ -26,7 +26,7 @@ trait Datatable {
2626
use Traits\Selectable;
2727
use Traits\Paginateable;
2828
use Traits\Filterable;
29-
29+
3030
/**
3131
* Initialize the datatable trait
3232
*

src/DatatableServiceProvider.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public function boot() {
2323
});
2424
}
2525

26-
2726
/**
2827
* Register the service provider.
2928
*

src/Traits/Paginateable.php

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
/**
88
* @method int getPerPage()
99
*/
10-
trait Paginateable
11-
{
10+
trait Paginateable {
11+
12+
/** @var int */
13+
protected $maxPerPage = 100;
1214

1315
/** @var string */
1416
protected $paginateablePageKey = 'page';
@@ -18,25 +20,35 @@ trait Paginateable
1820

1921
/**
2022
* @param \Illuminate\Database\Eloquent\Builder $query
21-
* @param bool $simple
23+
* @param bool $forcePagination
24+
* @return \Illuminate\Database\Eloquent\Model[]|\Illuminate\Contracts\Pagination\Paginator|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
25+
*/
26+
public function scopePaginateable(Builder $query, $forcePagination = true) {
27+
if (request()->hasFilled([$this->paginateablePageKey, $this->paginateablePerPageKey])) {
28+
return $query->paginate(
29+
min(request()->input('per_page', $this->getPerPage()), $this->maxPerPage),
30+
['*'],
31+
$this->paginateablePageKey
32+
);
33+
}
34+
35+
return $forcePagination ? $query->paginate() : $query->get();
36+
}
37+
38+
/**
39+
* @param \Illuminate\Database\Eloquent\Builder $query
40+
* @param bool $forcePagination
2241
* @return \Illuminate\Database\Eloquent\Model[]|\Illuminate\Contracts\Pagination\Paginator|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
2342
*/
24-
public function scopePaginateable(Builder $query, $simple = false)
25-
{
26-
if (request()->hasFilled([$this->paginateablePageKey, $this->paginateablePerPageKey]))
27-
{
28-
return $simple ?
29-
$query->simplePaginate(
30-
request()->input('per_page', $this->getPerPage()),
31-
['*'],
32-
$this->paginateablePageKey
33-
) : $query->paginate(
34-
request()->input('per_page', $this->getPerPage()),
35-
['*'],
36-
$this->paginateablePageKey
37-
);
43+
public function scopeSimplePaginateable(Builder $query, $forcePagination = true) {
44+
if (request()->hasFilled([$this->paginateablePageKey, $this->paginateablePerPageKey])) {
45+
return $query->simplePaginate(
46+
min(request()->input('per_page', $this->getPerPage()), $this->maxPerPage),
47+
['*'],
48+
$this->paginateablePageKey
49+
);
3850
}
3951

40-
return $query->get();
52+
return $forcePagination ? $query->simplePaginate() : $query->get();
4153
}
4254
}

0 commit comments

Comments
 (0)