Skip to content

Commit 39bd284

Browse files
committed
- Added upgrade to v3
1 parent 6a8e62a commit 39bd284

File tree

6 files changed

+87
-23
lines changed

6 files changed

+87
-23
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"extra": {
3232
"branch-alias": {
33-
"dev-main": "2.0-dev"
33+
"dev-main": "3.0-dev"
3434
},
3535
"laravel": {
3636
"providers": [

docs/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "Query Builder",
33
"description": "Modifications on top of spatie/query-builder",
4-
"version": "1"
4+
"version": "3"
55
}

docs/migration.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
title: Migration from v2
3+
sidebar_position: 1.2
4+
---
5+
6+
In v3, the access level of the `ApiBaseController` abstract methods have been changed from `protected` to `public`.
7+
8+
```php
9+
// In v2
10+
11+
/**
12+
* Get the allowed sorts
13+
*
14+
* @return array
15+
*/
16+
protected function getAllowedSorts(): array
17+
{
18+
return [
19+
'id',
20+
'name',
21+
'slug',
22+
'created_at',
23+
'updated_at',
24+
];
25+
}
26+
27+
// In v3
28+
29+
/**
30+
* Get the allowed sorts
31+
*
32+
* @return array
33+
*/
34+
public function getAllowedSorts(): array
35+
{
36+
return [
37+
'id',
38+
'name',
39+
'slug',
40+
'created_at',
41+
'updated_at',
42+
];
43+
}
44+
```
45+
46+
So, you will need to change the access level of the following methods of your API controllers.
47+
48+
49+
```php
50+
getBaseQuery(): Builder;
51+
getAllowedFields(): array;
52+
getAllowedIncludes(): array;
53+
getAllowedAppends(): array;
54+
getAllowedSorts(): array;
55+
getDefaultSort(): string;
56+
getAllowedFilters(): array;
57+
getShowAllowedAppends(): array
58+
getAllShowAllowedAppends(): array
59+
getShowAllowedAppendAttributes(): array
60+
getAllowedAppendAttributes(): array
61+
getIndexAllowedFields(): array
62+
getFieldsToAlwaysInclude(): array
63+
allowUnlimitedResultsPerPage(): bool
64+
```

src/Concerns/IsApiController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ trait IsApiController
3535
*
3636
* @return bool
3737
*/
38-
protected function allowUnlimitedResultsPerPage(): bool
38+
public function allowUnlimitedResultsPerPage(): bool
3939
{
4040
return property_exists($this, 'allow_unlimited_results') ? $this->allow_unlimited_results : false;
4141
}
@@ -176,7 +176,7 @@ protected function getIndexValidation(): array
176176
/**
177177
* Get the fields to always include
178178
*/
179-
protected function getFieldsToAlwaysInclude(): array
179+
public function getFieldsToAlwaysInclude(): array
180180
{
181181
return [
182182
'id'
@@ -186,7 +186,7 @@ protected function getFieldsToAlwaysInclude(): array
186186
/**
187187
* Get the index allowed fields
188188
*/
189-
protected function getIndexAllowedFields(): array
189+
public function getIndexAllowedFields(): array
190190
{
191191
return $this->getAllowedFields();
192192
}
@@ -275,7 +275,7 @@ protected function modifyQuery(\Spatie\QueryBuilder\QueryBuilder $query): \Spati
275275
*
276276
* @return array
277277
*/
278-
protected function getAllowedAppendAttributes(): array
278+
public function getAllowedAppendAttributes(): array
279279
{
280280
return Arr::rootKeys($this->getAllowedAppends());
281281
}
@@ -285,15 +285,15 @@ protected function getAllowedAppendAttributes(): array
285285
*
286286
* @return array
287287
*/
288-
protected function getShowAllowedAppendAttributes(): array
288+
public function getShowAllowedAppendAttributes(): array
289289
{
290290
return Arr::rootKeys($this->getAllShowAllowedAppends());
291291
}
292292

293293
/**
294294
* Get the show allowed appends
295295
*/
296-
protected function getAllShowAllowedAppends(): array
296+
public function getAllShowAllowedAppends(): array
297297
{
298298
if (! $this->getShowAllowedAppends()) {
299299
return $this->getAllowedAppends();
@@ -309,7 +309,7 @@ protected function getAllShowAllowedAppends(): array
309309
/**
310310
* Get the allowed appends for only show endpoint
311311
*/
312-
protected function getShowAllowedAppends(): array
312+
public function getShowAllowedAppends(): array
313313
{
314314
return [];
315315
}

src/Http/Controllers/ApiBaseController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,35 @@ public function __construct(QueryBuilderRequest $request)
2929
/**
3030
* Get the base query
3131
*/
32-
protected abstract function getBaseQuery(): Builder;
32+
public abstract function getBaseQuery(): Builder;
3333

3434
/**
3535
* Get the fields
3636
*/
37-
protected abstract function getAllowedFields(): array;
37+
public abstract function getAllowedFields(): array;
3838

3939
/**
4040
* Get the includes
4141
*/
42-
protected abstract function getAllowedIncludes(): array;
42+
public abstract function getAllowedIncludes(): array;
4343

4444
/**
4545
* Get the allowed appends
4646
*/
47-
protected abstract function getAllowedAppends(): array;
47+
public abstract function getAllowedAppends(): array;
4848

4949
/**
5050
* Get the allowed sorts
5151
*/
52-
protected abstract function getAllowedSorts(): array;
52+
public abstract function getAllowedSorts(): array;
5353

5454
/**
5555
* Get the default sort
5656
*/
57-
protected abstract function getDefaultSort(): string;
57+
public abstract function getDefaultSort(): string;
5858

5959
/**
6060
* Get the allowed filters
6161
*/
62-
protected abstract function getAllowedFilters(): array;
62+
public abstract function getAllowedFilters(): array;
6363
}

tests/Controllers/ProductsController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ProductsController extends ApiController
1414
*
1515
* @return Builder
1616
*/
17-
protected function getBaseQuery(): Builder
17+
public function getBaseQuery(): Builder
1818
{
1919
return Product::query();
2020
}
@@ -24,7 +24,7 @@ protected function getBaseQuery(): Builder
2424
*
2525
* @return array
2626
*/
27-
protected function getAllowedFields(): array
27+
public function getAllowedFields(): array
2828
{
2929
return array_diff(\Schema::getColumnListing('products'), (new Product)->getHidden());
3030
}
@@ -34,7 +34,7 @@ protected function getAllowedFields(): array
3434
*
3535
* @return array
3636
*/
37-
protected function getAllowedIncludes(): array
37+
public function getAllowedIncludes(): array
3838
{
3939
return [
4040
'brand',
@@ -46,7 +46,7 @@ protected function getAllowedIncludes(): array
4646
*
4747
* @return array
4848
*/
49-
protected function getAllowedAppends(): array
49+
public function getAllowedAppends(): array
5050
{
5151
return [
5252
'formatted_name' => [
@@ -60,7 +60,7 @@ protected function getAllowedAppends(): array
6060
*
6161
* @return array
6262
*/
63-
protected function getAllowedSorts(): array
63+
public function getAllowedSorts(): array
6464
{
6565
return [
6666
'id',
@@ -76,7 +76,7 @@ protected function getAllowedSorts(): array
7676
*
7777
* @return string
7878
*/
79-
protected function getDefaultSort(): string
79+
public function getDefaultSort(): string
8080
{
8181
return 'name';
8282
}
@@ -86,7 +86,7 @@ protected function getDefaultSort(): string
8686
*
8787
* @return array
8888
*/
89-
protected function getAllowedFilters(): array
89+
public function getAllowedFilters(): array
9090
{
9191
return [
9292
'name',

0 commit comments

Comments
 (0)