Skip to content

Commit 09ff352

Browse files
committed
- Added basic resource controller docs
1 parent f07e63f commit 09ff352

File tree

11 files changed

+260
-28
lines changed

11 files changed

+260
-28
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/javaabu/query-builder.svg?style=flat-square)](https://packagist.org/packages/javaabu/query-builder)
44
[![Test Status](../../actions/workflows/run-tests.yml/badge.svg)](../../actions/workflows/run-tests.yml)
5-
[![Quality Score](https://img.shields.io/scrutinizer/g/javaabu/query-builder.svg?style=flat-square)](https://scrutinizer-ci.com/g/javaabu/query-builder)
65
[![Total Downloads](https://img.shields.io/packagist/dt/javaabu/query-builder.svg?style=flat-square)](https://packagist.org/packages/javaabu/query-builder)
76

87
Modifications on top of spatie/query-builder

docs/about-us.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: About Us
3-
sidebar_position: 1.6
3+
sidebar_position: 1.7
44
---
55

66
[Javaabu](https://javaabu.com) is a web design agency based in the Maldives.

docs/basic-usage/_category_.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"position": 1.3,
2+
"position": 1.4,
33
"label": "Basic Usage",
44
"collapsible": true,
55
"collapsed": false,
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
---
2+
title: Basic resource controller
3+
sidebar_position: 1
4+
---
5+
6+
To create a basic API resource controller using this package, create a controller that extends the `Javaabu\QueryBuilder\Http\Controllers\ApiController` class.
7+
8+
```php
9+
<?php
10+
11+
namespace App\Http\Controllers\Api;
12+
13+
use Javaabu\QueryBuilder\Http\Controllers\ApiController;
14+
use App\Models\Product;
15+
use Illuminate\Database\Eloquent\Builder;
16+
use Spatie\QueryBuilder\AllowedFilter;
17+
18+
class ProductsController extends ApiController
19+
{
20+
/**
21+
* Get the base query
22+
*
23+
* @return Builder
24+
*/
25+
public function getBaseQuery(): Builder
26+
{
27+
return Product::query();
28+
}
29+
30+
/**
31+
* Get the allowed fields
32+
*
33+
* @return array
34+
*/
35+
public function getAllowedFields(): array
36+
{
37+
return array_diff(\Schema::getColumnListing('products'), (new Product)->getHidden());
38+
}
39+
40+
/**
41+
* Get the allowed includes
42+
*
43+
* @return array
44+
*/
45+
public function getAllowedIncludes(): array
46+
{
47+
return [
48+
'category'
49+
];
50+
}
51+
52+
/**
53+
* Get the allowed appends
54+
*
55+
* @return array
56+
*/
57+
public function getAllowedAppends(): array
58+
{
59+
return [
60+
'formatted_name' => [
61+
'name',
62+
'price'
63+
],
64+
65+
'formatted_price'
66+
];
67+
}
68+
69+
/**
70+
* Get the allowed sorts
71+
*
72+
* @return array
73+
*/
74+
public function getAllowedSorts(): array
75+
{
76+
return [
77+
'id',
78+
'created_at',
79+
'updated_at',
80+
'slug',
81+
'name',
82+
];
83+
}
84+
85+
/**
86+
* Get the default sort
87+
*
88+
* @return string
89+
*/
90+
public function getDefaultSort(): string
91+
{
92+
return '-created_at';
93+
}
94+
95+
/**
96+
* Get the allowed filters
97+
*
98+
* @return array
99+
*/
100+
public function getAllowedFilters(): array
101+
{
102+
return [
103+
'id',
104+
'slug',
105+
'name',
106+
AllowedFilter::scope('search'),
107+
];
108+
}
109+
}
110+
```
111+
112+
The controller will have an `index` and `show` method which you can use to define your routes in your `api.php` route file.
113+
114+
```php
115+
// in api.php route file
116+
117+
/**
118+
* Products
119+
*/
120+
Route::get('products', [\App\Controllers\Api\ProductsController::class, 'index'])->name('products.index');
121+
Route::get('products/{id}', [\App\Controllers\Api\ProductsController::class, 'show'])->name('products.show');
122+
```
123+
124+
The following methods will have to be implemented in your controller:
125+
126+
## getBaseQuery()
127+
128+
This method used to define your base query. It should return a `Illuminate\Database\Eloquent\Builder` instance.
129+
This builder instance will be passed to the `Javaabu\QueryBuilder\QueryBuilder\QueryBuilder::for()` method.
130+
131+
```php
132+
public function getBaseQuery(): Builder
133+
{
134+
return Product::query();
135+
}
136+
```
137+
138+
## getAllowedFields()
139+
140+
Used to define which fields that users will be allowed to request through the `?fields=` query parameter.
141+
If the user doesn't include the `fields` parameter, then all fields defined here will be included by default.
142+
143+
```php
144+
public function getAllowedFields(): array
145+
{
146+
return array_diff(\Schema::getColumnListing('products'), (new Product)->getHidden());
147+
}
148+
```
149+
150+
## getAllowedIncludes()
151+
152+
Used to define which relations that users will be allowed to request through the `?include=` query parameter.
153+
If the user doesn't include the `include` parameter, then all relations defined here will be included by default.
154+
To not include any relation in a request, the user should submit a blank `?include=` parameter.
155+
156+
```php
157+
public function getAllowedIncludes(): array
158+
{
159+
return [
160+
'category'
161+
];
162+
}
163+
```
164+
165+
## getAllowedAppends()
166+
167+
Used to define which accessor attibutes that users will be allowed to request through the `?append=` query parameter.
168+
If the user doesn't include the `append` parameter, then all appends defined here will be included by default.
169+
To not include any append in a request, the user should submit a blank `?append=` parameter.
170+
171+
Note that append fields can also be requested through the `fields` query parameter as well.
172+
173+
```php
174+
public function getAllowedAppends(): array
175+
{
176+
return [
177+
'formatted_price'
178+
];
179+
}
180+
```
181+
182+
If an append depends on other database columns, then you can specify those fields as an array.
183+
For example, `formatted_name` of the product might be `':name (:price)`. In this case, both `name` and `price` columns are required to generate the `formatted_name`.
184+
If you do not include these columns, the user will get a blank value for `formatted_name` if they don't specifically include `name` and `price` in the fields, when doing a request like `?fields=id,formatted_name`.
185+
186+
```php
187+
public function getAllowedAppends(): array
188+
{
189+
return [
190+
'formatted_name' => [
191+
'name',
192+
'price'
193+
],
194+
];
195+
}
196+
```
197+
198+
## getAllowedSorts()
199+
200+
Defines which fields that the user can sort the records by using the `?sort=` query param. Applies only to the `index` method.
201+
To sort in descending order, users can append a `-` to the field name, e.g. `?sort=-created_at` in the query parameter.
202+
To sort by multiple fields, users can provide a comma-separated list, e.g. `?sort=id,-created_at`
203+
204+
```php
205+
public function getAllowedSorts(): array
206+
{
207+
return [
208+
'id',
209+
'created_at',
210+
'updated_at',
211+
'slug',
212+
'name',
213+
];
214+
}
215+
```
216+
217+
## getDefaultSort()
218+
219+
Defines the default sort to apply if the user doesn't provide any `sort` parameter.
220+
221+
```php
222+
public function getDefaultSort(): string
223+
{
224+
return '-created_at';
225+
}
226+
```
227+
228+
Return an empty string if you don't want any default sort applied.
229+
230+
```php
231+
public function getDefaultSort(): string
232+
{
233+
return '';
234+
}
235+
```
236+
237+
## getAllowedFilters()
238+
239+
Defines the filters that users can apply using the `?filter[]` query parameter.
240+
241+
```php
242+
public function getAllowedFilters(): array
243+
{
244+
return [
245+
'id',
246+
'slug',
247+
'name',
248+
AllowedFilter::scope('search'),
249+
];
250+
}
251+
```

docs/basic-usage/how-to-use-feature.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Changelog
3-
sidebar_position: 1.5
3+
sidebar_position: 1.6
44
---
55

66
All notable changes to this package are documented on [GitHub](https://github.com/Javaabu/query-builder/blob/main/CHANGELOG.md)

docs/installation-and-setup.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,3 @@ You can install the package via composer:
88
```bash
99
composer require javaabu/query-builder
1010
```
11-
12-
# Publishing the config file
13-
14-
Publishing the config file is optional:
15-
16-
```bash
17-
php artisan vendor:publish --provider="Javaabu\QueryBuilder\QueryBuilderServiceProvider" --tag="query-builder-config"
18-
```
19-
20-
This is the default content of the config file:
21-
22-
```php
23-
// TODO
24-
```

docs/intro.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ sidebar_position: 1.0
55

66
# Query Builder
77

8+
First, refer to the [spatie/query-builder](https://github.com/spatie/laravel-query-builder) docs for full usage on the Query Builder package.
89

9-
[Query Builder](https://github.com/Javaabu/query-builder) Modifications on top of spatie/query-builder.
10+
[Query Builder](https://github.com/Javaabu/query-builder) Modifications on top of [spatie/query-builder](https://github.com/spatie/laravel-query-builder).
1011
Provides helper classes for creating API controllers and supports [Scribe](https://github.com/knuckleswtf/scribe/) for automatically generating API docs.
1112

1213
For example, if you have a `Product` model, you can create an API controller like so:

docs/questions-and-security.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Questions and Security
3-
sidebar_position: 1.4
3+
sidebar_position: 1.5
44
---
55

66
Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving this package? Feel free to create an [issue](../../issues) on GitHub, we'll try to address it as soon as possible.

docs/upgrade-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Upgrade Guide
3-
sidebar_position: 1.2
3+
sidebar_position: 1.3
44
---
55

66
## Migration from v2 to v3

0 commit comments

Comments
 (0)