1 | <?php |
||
11 | class QueryBuilder extends Builder |
||
12 | { |
||
13 | /** @var \Illuminate\Support\Collection */ |
||
14 | protected $allowedFilters; |
||
15 | |||
16 | /** @var string|null */ |
||
17 | protected $defaultSort; |
||
18 | |||
19 | /** @var \Illuminate\Support\Collection */ |
||
20 | protected $allowedSorts; |
||
21 | |||
22 | /** @var \Illuminate\Support\Collection */ |
||
23 | protected $allowedIncludes; |
||
24 | |||
25 | /** @var \Illuminate\Http\Request */ |
||
26 | protected $request; |
||
27 | |||
28 | public function __construct(Builder $builder, ?Request $request = null) |
||
44 | |||
45 | /** |
||
46 | * Create a new QueryBuilder for a request and model. |
||
47 | * |
||
48 | * @param string|\Illuminate\Database\Query\Builder $baseQuery Model class or base query builder |
||
49 | * @param Request $request |
||
50 | * |
||
51 | * @return \Spatie\QueryBuilder\QueryBuilder |
||
52 | */ |
||
53 | public static function for($baseQuery, ?Request $request = null): self |
||
61 | |||
62 | public function allowedFilters(...$filters): self |
||
78 | |||
79 | public function defaultSort($sort): self |
||
87 | |||
88 | public function allowedSorts(...$sorts): self |
||
104 | |||
105 | public function allowedIncludes(...$includes): self |
||
115 | |||
116 | protected function addFiltersToQuery(Collection $filters) |
||
124 | |||
125 | protected function findFilter(string $property) : ?Filter |
||
132 | |||
133 | protected function addSortsToQuery(Collection $sorts) |
||
134 | { |
||
135 | $sorts |
||
136 | ->each(function (string $sort) { |
||
137 | $descending = $sort[0] === '-'; |
||
138 | |||
139 | $key = ltrim($sort, '-'); |
||
140 | |||
141 | $this->orderBy($key, $descending ? 'desc' : 'asc'); |
||
142 | }); |
||
143 | } |
||
144 | |||
145 | protected function addIncludesToQuery(Collection $includes) |
||
155 | |||
156 | protected function guardAgainstUnknownFilters() |
||
168 | |||
169 | protected function guardAgainstUnknownSorts() |
||
181 | |||
182 | protected function guardAgainstUnknownIncludes() |
||
192 | } |
||
193 |