|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\QueryBuilder\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* PaginatesQuery provides enhanced query pagination mechanism. |
|
9
|
|
|
* |
|
10
|
|
|
* It allows control over page size via request parameter and pagination parameters setup via 'query-builder' config. |
|
11
|
|
|
* |
|
12
|
|
|
* @mixin \Spatie\QueryBuilder\QueryBuilder |
|
13
|
|
|
*/ |
|
14
|
|
|
trait PaginatesQuery |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Paginates the query. |
|
18
|
|
|
* @see \Illuminate\Database\Eloquent\Builder::paginate() |
|
19
|
|
|
* |
|
20
|
|
|
* @param array|int|null $perPage per page options, refer to {@see getPerPageValue()} for more details. |
|
21
|
|
|
* @param array $columns |
|
22
|
|
|
* @return \Illuminate\Contracts\Pagination\Paginator |
|
23
|
|
|
*/ |
|
24
|
|
|
public function pagination($perPage = null, $columns = ['*']) : Paginator |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->paginate($this->getPerPageValue($perPage), $columns, $this->getPageParameterName(), $this->getPageValue()); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Paginate the query into a simple paginator. |
|
31
|
|
|
* @see \Illuminate\Database\Eloquent\Builder::simplePaginate() |
|
32
|
|
|
* |
|
33
|
|
|
* @param array|int|null $perPage per page options, refer to {@see getPerPageValue()} for more details. |
|
34
|
|
|
* @param array $columns |
|
35
|
|
|
* @return \Illuminate\Contracts\Pagination\Paginator |
|
36
|
|
|
*/ |
|
37
|
|
|
public function simplePagination($perPage = null, $columns = ['*']) : Paginator |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->simplePaginate($this->getPerPageValue($perPage), $columns, $this->getPageParameterName(), $this->getPageValue()); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Extracts per page value from the related HTTP request. |
|
44
|
|
|
* |
|
45
|
|
|
* @param array|int|null $options if not set - use default options, if scalar - use it as default per page value. |
|
46
|
|
|
* If array - use as options with following keys: |
|
47
|
|
|
* |
|
48
|
|
|
* - 'default' - int, default per page value. |
|
49
|
|
|
* - 'min' - int, min allowed per page value. |
|
50
|
|
|
* - 'max' - int, max allowed per page value. |
|
51
|
|
|
* |
|
52
|
|
|
* @return int per page value. |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function getPerPageValue($options = null): int |
|
55
|
|
|
{ |
|
56
|
|
|
$defaultOptions = [ |
|
57
|
|
|
'default' => config('query-builder.pagination.per-page.default', 15), |
|
58
|
|
|
'min' => config('query-builder.pagination.per-page.min', 1), |
|
59
|
|
|
'max' => config('query-builder.pagination.per-page.max', 50), |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
if (is_array($options)) { |
|
63
|
|
|
$options = array_merge($defaultOptions, $options); |
|
64
|
|
|
} elseif ($options === null) { |
|
65
|
|
|
$options = $defaultOptions; |
|
66
|
|
|
} else { |
|
67
|
|
|
$options = array_merge($defaultOptions, ['default' => $options]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$options = array_map(function ($value) { |
|
71
|
|
|
return (int) $value; |
|
72
|
|
|
}, $options); |
|
73
|
|
|
|
|
74
|
|
|
$perPageParameterName = config('query-builder.parameters.per-page', 'per-page'); |
|
75
|
|
|
|
|
76
|
|
|
$perPage = (int) $this->request->query($perPageParameterName, $options['default']); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
if ($perPage < $options['min']) { |
|
79
|
|
|
return $options['min']; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($perPage > $options['max']) { |
|
83
|
|
|
return $options['max']; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $perPage; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return string HTTP query parameter name for page number. |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function getPageParameterName() : string |
|
93
|
|
|
{ |
|
94
|
|
|
return config('query-builder.parameters.page', 'page'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return int|null page number passed within associated HTTP request. |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getPageValue() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->request->query($this->getPageParameterName()); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.