@@ -64,7 +64,7 @@ Admin API will try to guess attribute types and generate field names by capitali
6464* belongsTo, hasMany and belongsToMany relations will just work
6565
6666You can also implement a ` MrTimofey\LaravelAdminApi\Contracts\ConfiguresAdminHandler ` interface and define a
67- ` configuresAdminHandler ($handler)` method to make things more controllable.
67+ ` configureAdminHandler ($handler)` method to make things more controllable.
6868
6969Available field types and their options are described in
7070[ vue-admin-front field types docs] ( https://mr-timofey.gitbooks.io/vue-admin/content/fields.html#available-field-types ) .
@@ -86,7 +86,7 @@ class Post extends Model implements ConfiguresAdminHandler
8686{
8787 protected $casts = [
8888 'published' => 'bool'
89- ];
89+ ];
9090
9191 public function category(): BelongsTo
9292 {
@@ -103,103 +103,103 @@ class Post extends Model implements ConfiguresAdminHandler
103103 return $q->whereRaw('draft IS TRUE');
104104 }
105105
106- public function configureAdminHandler(ModelHandler $handler): void
107- {
108- $handler->setTitle('Posts')
109- ->setCreateTitle('Creating new post')
110- // placeholders can be used, see more: https://mr-timofey.gitbooks.io/vue-admin/placeholders.html
111- ->setItemTitle('Editing post #{{ id }}')
112-
113- // allow only these methods (everything is allowed by default)
114- ->allowActions(['index', 'item', 'create', 'update', 'destroy'])
115- // ...or use policies instead
116- ->usePolicies(true,
117- // optional policy method prefix (Policy::adminActionIndex, Policy::adminActionCreate, etc.)
118- 'adminAction')
119-
120- ->addPreQueryModifier(function(Builder $q, Request $req): void {
121- // modify index query just after Model::newQuery() is called
122- })
123- ->addPostQueryModifier(function(Builder $q,Request $req): void {
124- // modify index query just before execution
125- })
126- // automatically search with LIKE
127- ->setSearchableFields(['title', 'summary'])
128- // ...or/and set custom search callback
129- ->setSearchCallback(function(
130- Builder $q,
131- Request $req,
132- array $searchableFields): void {
133- $q->searchLikeAGod($req->get('search'));
134- })
135-
136- // index page filters
137- ->setFilterFields([
138- // auto relation filter
139- 'category',
140- // see more about available prefix modifiers in ModelHandler::applyFilters sources
141- '>~created_at' => [
142- 'title' => 'Created after',
143- 'type' => 'datetime'
144- ],
145- // checkbox, applies scopeOnlyDrafts when checked
146- 'drafts' => [
147- 'scope' => 'onlyDrafts',
148- 'type' => 'switcher'
149- ]
150- ])
151-
152- ->setIndexFields([
153- 'id',
154- 'title',
155- // will be automatically formatted as datetime
156- 'created_at'
157- ])
158-
159- ->setItemFields([
160- 'title',
161- // this just works
162- 'category', // categories should be added to api_admin.models config
163- // this should just work as well but we want some customizations
164- 'tags' => [
165- 'title' => 'Attach tags',
166- // 'type' => 'relation', // not necessary if field name is same as a relation method
167- // 'entity' => 'tags', // tags should be added to api_admin.models config
168- // placeholders can be used, see more: https://mr-timofey.gitbooks.io/vue-admin/placeholders.html
169- 'display' => '{{ name }}',
170- 'allowCreate' => true,
171- 'createField' => 'name',
172- 'createDefaults' => ['description' => 'New tag'],
173- 'queryParams' => [
174- 'sort' => ['sort' => 'asc']
175- ]
176- ],
177- 'content' => ['type' => 'wysiwyg'],
178- 'published' // $casts will automatically set the right field type for you
179- ])
180-
181- // you can set validation rules
182- ->setValidationRules([
183- 'tags' => ['array', 'between:3,8'],
184- 'category' => ['required']
185- ])
186- // ...or/and custom validation callback
187- ->setValidationCallback(
188- /**
189- * @throws \Illuminate\Validation\ValidationException
190- */
191- function(
192- \Illuminate\Http\Request $req,
193- array $rules,
194- array $messages,
195- // gathered from item fields configuration
196- array $titles
197- ) {
198- $req->validate([ /* whatever */ ]);
199- })
200- ->setValidationMessages([
201- 'category.required' => 'No category - no post'
202- ]);
203- }
106+ public function configureAdminHandler(ModelHandler $handler): void
107+ {
108+ $handler->setTitle('Posts')
109+ ->setCreateTitle('Creating new post')
110+ // placeholders can be used, see more: https://mr-timofey.gitbooks.io/vue-admin/placeholders.html
111+ ->setItemTitle('Editing post #{{ id }}')
112+
113+ // allow only these methods (everything is allowed by default)
114+ ->allowActions(['index', 'item', 'create', 'update', 'destroy'])
115+ // ...or use policies instead
116+ ->usePolicies(true,
117+ // optional policy method prefix (Policy::adminActionIndex, Policy::adminActionCreate, etc.)
118+ 'adminAction')
119+
120+ ->addPreQueryModifier(function(Builder $q, Request $req): void {
121+ // modify index query just after Model::newQuery() is called
122+ })
123+ ->addPostQueryModifier(function(Builder $q,Request $req): void {
124+ // modify index query just before execution
125+ })
126+ // automatically search with LIKE
127+ ->setSearchableFields(['title', 'summary'])
128+ // ...or/and set custom search callback
129+ ->setSearchCallback(function(
130+ Builder $q,
131+ Request $req,
132+ array $searchableFields): void {
133+ $q->searchLikeAGod($req->get('search'));
134+ })
135+
136+ // index page filters
137+ ->setFilterFields([
138+ // auto relation filter
139+ 'category',
140+ // see more about available prefix modifiers in ModelHandler::applyFilters sources
141+ '>~created_at' => [
142+ 'title' => 'Created after',
143+ 'type' => 'datetime'
144+ ],
145+ // checkbox, applies scopeOnlyDrafts when checked
146+ 'drafts' => [
147+ 'scope' => 'onlyDrafts',
148+ 'type' => 'switcher'
149+ ]
150+ ])
151+
152+ ->setIndexFields([
153+ 'id',
154+ 'title',
155+ // will be automatically formatted as datetime
156+ 'created_at'
157+ ])
158+
159+ ->setItemFields([
160+ 'title',
161+ // this just works
162+ 'category', // categories should be added to api_admin.models config
163+ // this should just work as well but we want some customizations
164+ 'tags' => [
165+ 'title' => 'Attach tags',
166+ // 'type' => 'relation', // not necessary if field name is same as a relation method
167+ // 'entity' => 'tags', // tags should be added to api_admin.models config
168+ // placeholders can be used, see more: https://mr-timofey.gitbooks.io/vue-admin/placeholders.html
169+ 'display' => '{{ name }}',
170+ 'allowCreate' => true,
171+ 'createField' => 'name',
172+ 'createDefaults' => ['description' => 'New tag'],
173+ 'queryParams' => [
174+ 'sort' => ['sort' => 'asc']
175+ ]
176+ ],
177+ 'content' => ['type' => 'wysiwyg'],
178+ 'published' // $casts will automatically set the right field type for you
179+ ])
180+
181+ // you can set validation rules
182+ ->setValidationRules([
183+ 'tags' => ['array', 'between:3,8'],
184+ 'category' => ['required']
185+ ])
186+ // ...or/and custom validation callback
187+ ->setValidationCallback(
188+ /**
189+ * @throws \Illuminate\Validation\ValidationException
190+ */
191+ function(
192+ \Illuminate\Http\Request $req,
193+ array $rules,
194+ array $messages,
195+ // gathered from item fields configuration
196+ array $titles
197+ ) {
198+ $req->validate([ /* whatever */ ]);
199+ })
200+ ->setValidationMessages([
201+ 'category.required' => 'No category - no post'
202+ ]);
203+ }
204204}
205205```
0 commit comments