Skip to content

Commit 0176e88

Browse files
committed
readme improvements
1 parent 306538f commit 0176e88

File tree

1 file changed

+62
-9
lines changed

1 file changed

+62
-9
lines changed

README.md

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ You can also implement a `MrTimofey\LaravelAdminApi\Contracts\ConfiguresAdminHan
6868

6969
Available 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).
71+
Same for fields formatting for model index page
72+
[vue-admin-front display types docs](https://mr-timofey.gitbooks.io/vue-admin/content/displays.html#available-display-types).
7173

7274
Usage example:
7375

@@ -119,9 +121,15 @@ class Post extends Model implements ConfiguresAdminHandler
119121

120122
->addPreQueryModifier(function(Builder $q, Request $req): void {
121123
// modify index query just after Model::newQuery() is called
124+
$user = $req->user();
125+
if ($user->role !== 'god') {
126+
$q->where('author_user_id', $user->getKey());
127+
}
122128
})
123129
->addPostQueryModifier(function(Builder $q,Request $req): void {
124130
// modify index query just before execution
131+
// useful if you want to set default sort order
132+
$q->orderByDesc('created_at');
125133
})
126134
// automatically search with LIKE
127135
->setSearchableFields(['title', 'summary'])
@@ -137,7 +145,7 @@ class Post extends Model implements ConfiguresAdminHandler
137145
->setFilterFields([
138146
// auto relation filter
139147
'category',
140-
// see more about available prefix modifiers in ModelHandler::applyFilters sources
148+
// see more about available prefix modifiers in ModelHandler::applyFilters phpdoc
141149
'>~created_at' => [
142150
'title' => 'Created after',
143151
'type' => 'datetime'
@@ -148,14 +156,17 @@ class Post extends Model implements ConfiguresAdminHandler
148156
'type' => 'switcher'
149157
]
150158
])
151-
159+
160+
// index page table columns
152161
->setIndexFields([
153162
'id',
154163
'title',
155-
// will be automatically formatted as datetime
164+
// will be automatically formatted as datetime if $this->timestamps === true
165+
// or if $this->dates includes 'created_at' field
156166
'created_at'
157167
])
158-
168+
169+
// item creating/editing form fields
159170
->setItemFields([
160171
'title',
161172
// this just works
@@ -167,23 +178,33 @@ class Post extends Model implements ConfiguresAdminHandler
167178
// 'entity' => 'tags', // tags should be added to api_admin.models config
168179
// placeholders can be used, see more: https://mr-timofey.gitbooks.io/vue-admin/placeholders.html
169180
'display' => '{{ name }}',
181+
// relation widget will allow user to create new tags in-place
170182
'allowCreate' => true,
183+
// this field will be filled with the widget's search box input text
171184
'createField' => 'name',
185+
// fill some other fields with fixed values while creating new tag
172186
'createDefaults' => ['description' => 'New tag'],
187+
// customize suggestions query
173188
'queryParams' => [
174189
'sort' => ['sort' => 'asc']
175190
]
176191
],
177192
'content' => ['type' => 'wysiwyg'],
178-
'published' // $casts will automatically set the right field type for you
193+
// $casts => ['published' => 'bool'] will automatically set the right field type for you (checkbox)
194+
'published'
179195
])
180-
181-
// you can set validation rules
196+
197+
// creating/editing validation rules (use $this to refer to the currently editing model instance)
182198
->setValidationRules([
183199
'tags' => ['array', 'between:3,8'],
184-
'category' => ['required']
200+
'category' => ['required'],
201+
'some_unique_field' => [
202+
'required',
203+
'unique,' . $this->getTable() . ',some_unique_field' .
204+
($this->exists ? (',' . $this->getKey() . ',' . $this->getKeyName()) : '')
205+
]
185206
])
186-
// ...or/and custom validation callback
207+
// ...or/and defined custom validation callback
187208
->setValidationCallback(
188209
/**
189210
* @throws \Illuminate\Validation\ValidationException
@@ -197,9 +218,41 @@ class Post extends Model implements ConfiguresAdminHandler
197218
) {
198219
$req->validate([ /* whatever */ ]);
199220
})
221+
// override default error messages
200222
->setValidationMessages([
201223
'category.required' => 'No category - no post'
202224
]);
203225
}
204226
}
205227
```
228+
## Events
229+
230+
Every action within an administrative panel can be tracked and processed with the Laravel's event system.
231+
Available events:
232+
233+
```php
234+
<?php
235+
236+
namespace MrTimofey\LaravelAdminApi\Events;
237+
238+
// abstract base class for all events (holds user identifier)
239+
ModelEvent::class;
240+
241+
// single model instance action events
242+
SingleModelEvent::class; // abstract base class for single model instance action events (holds item identifier)
243+
ModelCreated::class;
244+
ModelUpdated::class; // holds attributes changes, more info in phpdoc of this class
245+
ModelDestroyed::class;
246+
247+
// bulk destroy (holds destroyed instances' identifiers)
248+
BulkDestroyed::class;
249+
```
250+
251+
### Tracking changes for ModelUpdated event
252+
253+
By default `ModelUpdated` will track only instance attributes (using Eloquent's `Model::getDirty()`)
254+
method and relation changes.
255+
256+
You can implement `MrTimofey\LaravelAdminApi\Contracts\HasCustomChanges` interface and define `getCustomChanges()`
257+
method to enrich `ModelUpdated::$changes` field with any additional information you want to track. Default format
258+
is to return array `['field_name' => [$oldValue, $newValue], ...]`.

0 commit comments

Comments
 (0)