Skip to content

Commit f07e63f

Browse files
committed
- Testing docs
1 parent 4340284 commit f07e63f

File tree

1 file changed

+104
-4
lines changed

1 file changed

+104
-4
lines changed

docs/intro.md

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,110 @@ sidebar_position: 1.0
55

66
# Query Builder
77

8-
:::danger
98

10-
This package is currently under development. If anything works, that's a surprise.
9+
[Query Builder](https://github.com/Javaabu/query-builder) Modifications on top of spatie/query-builder.
10+
Provides helper classes for creating API controllers and supports [Scribe](https://github.com/knuckleswtf/scribe/) for automatically generating API docs.
1111

12-
:::
12+
For example, if you have a `Product` model, you can create an API controller like so:
1313

14-
[Query Builder](https://github.com/Javaabu/query-builder) Modifications on top of spatie/query-builder.
14+
```php
15+
<?php
16+
17+
namespace App\Http\Controllers\Api;
18+
19+
use Javaabu\QueryBuilder\Http\Controllers\ApiController;
20+
use App\Models\Product;
21+
use Illuminate\Database\Eloquent\Builder;
22+
use Spatie\QueryBuilder\AllowedFilter;
23+
24+
class ProductsController extends ApiController
25+
{
26+
/**
27+
* Get the base query
28+
*
29+
* @return Builder
30+
*/
31+
public function getBaseQuery(): Builder
32+
{
33+
return Product::query();
34+
}
35+
36+
/**
37+
* Get the allowed fields
38+
*
39+
* @return array
40+
*/
41+
public function getAllowedFields(): array
42+
{
43+
return array_diff(\Schema::getColumnListing('products'), (new Product)->getHidden());
44+
}
45+
46+
/**
47+
* Get the allowed includes
48+
*
49+
* @return array
50+
*/
51+
public function getAllowedIncludes(): array
52+
{
53+
return [
54+
'category'
55+
];
56+
}
57+
58+
/**
59+
* Get the allowed appends
60+
*
61+
* @return array
62+
*/
63+
public function getAllowedAppends(): array
64+
{
65+
return [
66+
'formatted_name' => [
67+
'name',
68+
'price'
69+
]
70+
];
71+
}
72+
73+
/**
74+
* Get the allowed sorts
75+
*
76+
* @return array
77+
*/
78+
public function getAllowedSorts(): array
79+
{
80+
return [
81+
'id',
82+
'created_at',
83+
'updated_at',
84+
'slug',
85+
'name',
86+
];
87+
}
88+
89+
/**
90+
* Get the default sort
91+
*
92+
* @return string
93+
*/
94+
public function getDefaultSort(): string
95+
{
96+
return '-created_at';
97+
}
98+
99+
/**
100+
* Get the allowed filters
101+
*
102+
* @return array
103+
*/
104+
public function getAllowedFilters(): array
105+
{
106+
return [
107+
'id',
108+
'slug',
109+
'name',
110+
AllowedFilter::scope('search'),
111+
];
112+
}
113+
}
114+
```

0 commit comments

Comments
 (0)