AppServiceProvider.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Illuminate\Support\Facades\Log;
  5. use Illuminate\Database\Eloquent\Model;
  6. class AppServiceProvider extends ServiceProvider
  7. {
  8. /**
  9. * Register any application services.
  10. *
  11. * @return void
  12. */
  13. public function register()
  14. {
  15. }
  16. /**
  17. * Bootstrap any application services.
  18. *
  19. * @return void
  20. */
  21. public function boot()
  22. {
  23. //
  24. Model::created(function ($model) {
  25. $this->logDBOperation('created', $model);
  26. });
  27. Model::updated(function ($model) {
  28. $this->logDBOperation('updated', $model);
  29. });
  30. Model::deleted(function ($model) {
  31. $this->logDBOperation('deleted', $model);
  32. });
  33. }
  34. protected function logDBOperation($action, $model)
  35. {
  36. $table = $model->getTable();
  37. $data = $model->getAttributes();
  38. $log = [
  39. 'time' => now()->toDateTimeString(),
  40. 'action' => $action,
  41. 'table' => $table,
  42. 'data' => $data,
  43. ];
  44. Log::channel('dblog')->info('DB Operation', $log);
  45. }
  46. }