Skip to content

Commit 064436f

Browse files
committed
Fix innaccuracy in hooks examples.
Add PHPDoc blocks to sample code. `creating`, `updating`, and `saving` must always return `$data` array.
1 parent 966ddab commit 064436f

File tree

1 file changed

+111
-16
lines changed

1 file changed

+111
-16
lines changed

editor-events.md

Lines changed: 111 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,40 @@ Create action has the following event hooks:
1313
To use the event hook, just add the methods on your editor class.
1414

1515
```php
16+
/**
17+
* Event hook that is fired before creating a new record.
18+
*
19+
* @param \Illuminate\Database\Eloquent\Model $model Empty model instance.
20+
* @param array $data Attribute values array received from Editor.
21+
* @return array The updated attribute values array.
22+
*/
1623
public function creating(Model $model, array $data)
1724
{
18-
// Prior to version 1.8.0 of Laravel DataTables Editor the hook was not required
19-
// to return the $model.
20-
// In version 1.8.0+ the hook must return the $model instance:
21-
return $model;
25+
// Code can change the attribute values array before saving data to the
26+
// database.
27+
// Can be used to initialize values on new model.
28+
29+
// Since arrays are copied when passed by value, the function must return
30+
// the updated $data array
31+
return $data;
2232
}
2333

34+
/**
35+
* Event hook that is fired after a new record is created.
36+
*
37+
* @param \Illuminate\Database\Eloquent\Model $model The newly created model.
38+
* @param array $data Attribute values array received from `creating` or
39+
* `saving` hook.
40+
* @return \Illuminate\Database\Eloquent\Model Since version 1.8.0 it must
41+
* return the $model.
42+
*/
2443
public function created(Model $model, array $data)
2544
{
26-
// Prior to version 1.8.0 of Laravel DataTables Editor the hook was not required
27-
// to return the $model.
45+
// Can be used to mutate state of newly created model that is returned to
46+
// Editor.
47+
48+
// Prior to version 1.8.0 of Laravel DataTables Editor the hook was not
49+
// required to return the $model.
2850
// In version 1.8.0+ the hook must return the $model instance:
2951
return $model;
3052
}
@@ -35,37 +57,94 @@ public function created(Model $model, array $data)
3557

3658
Edit action has the following event hooks:
3759

38-
- `updating` event hook that is fired before updating a new record.
60+
- `updating` event hook that is fired before updating an existing record.
3961
- `updated` event hook that is fired after the record was updated.
4062

4163
To use the event hook, just add the methods on your editor class.
4264

4365
```php
66+
/**
67+
* Event hook that is fired before updating an existing record.
68+
*
69+
* @param \Illuminate\Database\Eloquent\Model $model Model instance retrived
70+
* retrived from database.
71+
* @param array $data Attribute values array received from Editor.
72+
* @return array The updated attribute values array.
73+
*/
4474
public function updating(Model $model, array $data) {
45-
// Prior to version 1.8.0 of Laravel DataTables Editor the hook was not required
46-
// to return the $model.
47-
// In version 1.8.0+ the hook must return the $model:
48-
return $model;
75+
// Can be used to modify the attribute values received from Editor before
76+
// applying changes to model.
77+
78+
// Since arrays are copied when passed by value, the function must return
79+
// the updated $data array
80+
return $data;
4981
}
5082

83+
/**
84+
* Event hook that is fired after the record was updated.
85+
*
86+
* @param \Illuminate\Database\Eloquent\Model $model Updated model instance.
87+
* @param array $data Attribute values array received from `updating` or
88+
* `saving` hook.
89+
* @return \Illuminate\Database\Eloquent\Model Since version 1.8.0 it must
90+
* return the $model.
91+
*/
5192
public function updated(Model $model, array $data) {
93+
// Can be used to mutate state of updated model that is returned to Editor.
94+
5295
// Prior to version 1.8.0 of Laravel DataTables Editor the hook was not required
5396
// to return the $model.
5497
// In version 1.8.0+ the hook must return the $model instance:
5598
return $model;
5699
}
57100
```
58101

59-
<a name="saved-event"></a>
60-
## Saved event
102+
<a name="save-events"></a>
103+
## Save events
61104

62-
In addition to create and edit events, the `saved` event hook is called after `created` and `saved`.
105+
In addition to create and edit events, the following save event hooks are available:
106+
107+
- `saving` event hook that is fired after `creating` and `updating` events, but
108+
before the model is saved to the database.
109+
- `saved` event hook that is fired after `created` and `updated` events.
63110

64111
To use the event hook, just add the method on your editor class:
65112

66113
```php
114+
/**
115+
* Event hook that is fired after `creating` and `updating` hooks, but before
116+
* the model is saved to the database.
117+
*
118+
* @param \Illuminate\Database\Eloquent\Model $model Empty model when creating;
119+
* Original model when updating.
120+
* @param array $data Attribute values array received from `creating` or
121+
* `updating` event hook.
122+
* @return array The updated attribute values array.
123+
*/
124+
public function saving(Model $model, array $data)
125+
{
126+
// The event hook can be used to modify the $data array that is used to
127+
// create or update the record.
128+
129+
// Since arrays are copied when passed by value, the function must return
130+
// the updated $data array
131+
return $data;
132+
}
133+
134+
/**
135+
* Event hook that is fired after `created` and `updated` events.
136+
*
137+
* @param \Illuminate\Database\Eloquent\Model $model The new model when
138+
* creating; the updated model when updating.
139+
* @param array $data Attribute values array received from `creating`,
140+
* `updating`, or `saving`.
141+
* @return \Illuminate\Database\Eloquent\Model Since version 1.8.0 it must
142+
* return the $model.
143+
*/
67144
public function saved(Model $model, array $data)
68145
{
146+
// Can be used to mutate state of updated model that is returned to Editor.
147+
69148
// Prior to version 1.8.0 of Laravel DataTables Editor the hook was not required
70149
// to return the $model.
71150
// In version 1.8.0+ the hook must return the $model instance:
@@ -84,15 +163,31 @@ Remove action has the following event hooks:
84163
To use the event hook, just add the methods on your editor class.
85164

86165
```php
166+
/**
167+
* Event hook that is fired before deleting an existing record.
168+
*
169+
* @param \Illuminate\Database\Eloquent\Model $model The original model
170+
* retrieved from database.
171+
* @param array $data Attribute values array received from Editor.
172+
* @return void
173+
*/
87174
public function deleting(Model $model, array $data) {
88175
// Record still exists in database. Code can be used to delete records from
89176
// child tables that don't specify cascade deletes on the foreign key
90177
// definition.
91178
}
92179

180+
/**
181+
* Event hook that is fired after deleting the record from database.
182+
*
183+
* @param \Illuminate\Database\Eloquent\Model $model The original model
184+
* retrieved from database.
185+
* @param array $data Attribute values array received from Editor.
186+
* @return void
187+
*/
93188
public function deleted(Model $model, array $data) {
94189
// Record no longer exists in database, but $model instance still contains
95-
// data as it was before deleting. Any instance state mutation will be
96-
// preserved and returned in the 'data' array of the response.
190+
// data as it was before deleting. Any changes to the $model instance will
191+
// be returned to Editor.
97192
}
98193
```

0 commit comments

Comments
 (0)