Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Traits;
- /**
- * Show the diff for the model
- */
- trait CanModelDiff
- {
- /**
- * Fetch a diff for the model's current state.
- */
- public function getDiff()
- {
- // get the old model
- $changed = $this->getDirty();
- // collect the new data
- $fix = is_null($this->fresh()) ? [''] : $this->fresh()->toArray();
- // find the properties that changed
- $before = array_intersect_key($fix, $changed);
- $after = $changed;
- // returns an array
- return compact('before', 'after');
- }
- }
- // USAGE NOTE: The diff feature can only be used during `eloquent.updating`
- // $changes = $model->getDiff();
- // // the activated property was changed
- // if (isset($changes['after']['activated'])) {
- // $model->notify(new ModelActivated());
- // }
- // // the email property was changed. email new owner?
- // if (isset($changes['after']['email'])) {
- // $model->notify(new ModelEmailUpdated());
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement