Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Traits;
  4.  
  5. /**
  6. * Show the diff for the model
  7. */
  8. trait CanModelDiff
  9. {
  10. /**
  11. * Fetch a diff for the model's current state.
  12. */
  13. public function getDiff()
  14. {
  15. // get the old model
  16. $changed = $this->getDirty();
  17. // collect the new data
  18. $fix = is_null($this->fresh()) ? [''] : $this->fresh()->toArray();
  19. // find the properties that changed
  20. $before = array_intersect_key($fix, $changed);
  21. $after = $changed;
  22.  
  23. // returns an array
  24. return compact('before', 'after');
  25. }
  26. }
  27.  
  28. // USAGE NOTE: The diff feature can only be used during `eloquent.updating`
  29.  
  30. // $changes = $model->getDiff();
  31. // // the activated property was changed
  32. // if (isset($changes['after']['activated'])) {
  33. // $model->notify(new ModelActivated());
  34. // }
  35.  
  36. // // the email property was changed. email new owner?
  37. // if (isset($changes['after']['email'])) {
  38. // $model->notify(new ModelEmailUpdated());
  39. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement