Skip to content

Commit 265cdff

Browse files
authored
Merge pull request yajra#53 from Xint0/52-Update-editor-events-code-samples-for-version-1.8.0
Update code samples with changes in version 1.8.0
2 parents 6438941 + 064436f commit 265cdff

File tree

1 file changed

+137
-7
lines changed

1 file changed

+137
-7
lines changed

editor-events.md

Lines changed: 137 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,42 @@ 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-
public function creating(Model $model, array $data) {
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+
*/
23+
public function creating(Model $model, array $data)
24+
{
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
1731
return $data;
1832
}
1933

20-
public function created(Model $model, array $data) {
21-
return $data;
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+
*/
43+
public function created(Model $model, array $data)
44+
{
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.
50+
// In version 1.8.0+ the hook must return the $model instance:
51+
return $model;
2252
}
2353
```
2454

@@ -27,37 +57,137 @@ public function created(Model $model, array $data) {
2757

2858
Edit action has the following event hooks:
2959

30-
- `updating` event hook that is fired before updating a new record.
60+
- `updating` event hook that is fired before updating an existing record.
3161
- `updated` event hook that is fired after the record was updated.
3262

3363
To use the event hook, just add the methods on your editor class.
3464

3565
```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+
*/
3674
public function updating(Model $model, array $data) {
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
3780
return $data;
3881
}
3982

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+
*/
4092
public function updated(Model $model, array $data) {
93+
// Can be used to mutate state of updated model that is returned to Editor.
94+
95+
// Prior to version 1.8.0 of Laravel DataTables Editor the hook was not required
96+
// to return the $model.
97+
// In version 1.8.0+ the hook must return the $model instance:
98+
return $model;
99+
}
100+
```
101+
102+
<a name="save-events"></a>
103+
## Save events
104+
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.
110+
111+
To use the event hook, just add the method on your editor class:
112+
113+
```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
41131
return $data;
42132
}
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+
*/
144+
public function saved(Model $model, array $data)
145+
{
146+
// Can be used to mutate state of updated model that is returned to Editor.
147+
148+
// Prior to version 1.8.0 of Laravel DataTables Editor the hook was not required
149+
// to return the $model.
150+
// In version 1.8.0+ the hook must return the $model instance:
151+
return $model;
152+
}
43153
```
44154

45155
<a name="remove-events"></a>
46156
## Remove Events
47157

48158
Remove action has the following event hooks:
49159

50-
- `deleting` event hook that is fired before deleting a new record.
160+
- `deleting` event hook that is fired before deleting a record.
51161
- `deleted` event hook that is fired after the record was deleted.
52162

53163
To use the event hook, just add the methods on your editor class.
54164

55165
```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+
*/
56174
public function deleting(Model $model, array $data) {
57-
return $data;
175+
// Record still exists in database. Code can be used to delete records from
176+
// child tables that don't specify cascade deletes on the foreign key
177+
// definition.
58178
}
59179

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+
*/
60188
public function deleted(Model $model, array $data) {
61-
return $data;
189+
// Record no longer exists in database, but $model instance still contains
190+
// data as it was before deleting. Any changes to the $model instance will
191+
// be returned to Editor.
62192
}
63193
```

0 commit comments

Comments
 (0)