AuthorityObserver.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Observers;
  3. use App\Authority;
  4. class AuthorityObserver
  5. {
  6. /**
  7. * Handle the authority "created" event.
  8. *
  9. * @param \App\Authority $authority
  10. * @return void
  11. */
  12. public function created(Authority $authority)
  13. {
  14. if (env('APP_ENV')!='production')return;
  15. $txt = "INSERT INTO authorities";
  16. $columns = "(";
  17. $values = "(";
  18. foreach ($authority->toArray() as $col=>$val){
  19. $columns .= "{$col},";
  20. $values .= "'{$val}',";
  21. }
  22. $columns = mb_substr($columns,0,-1);
  23. $values = mb_substr($values,0,-1);
  24. $txt .= "{$columns}) VALUES{$values});\r\n";
  25. $this->write($txt);
  26. }
  27. /**
  28. * Handle the authority "updated" event.
  29. *
  30. * @param \App\Authority $authority
  31. * @return void
  32. */
  33. public function updated(Authority $authority)
  34. {
  35. if (env('APP_ENV')!='production')return;
  36. $txt = "UPDATE authorities SET ";
  37. foreach ($authority->toArray() as $col=>$val)$txt .= "{$col}='{$val}',";
  38. $txt = mb_substr($txt,0,-1);
  39. $txt .= " WHERE id = {$authority->id};\r\n";
  40. $this->write($txt);
  41. }
  42. /**
  43. * Handle the authority "deleted" event.
  44. *
  45. * @param \App\Authority $authority
  46. * @return void
  47. */
  48. public function deleted(Authority $authority)
  49. {
  50. if (env('APP_ENV')!='production')return;
  51. $txt = "DELETE FROM authorities WHERE id = {$authority->id};\r\n";
  52. $this->write($txt);
  53. }
  54. private function write($txt)
  55. {
  56. $myfile = fopen(base_path()."\\database\\data\\authorities.data", "a+");
  57. fwrite($myfile, $txt);
  58. fclose($myfile);
  59. }
  60. }