AppServiceProvider.php 1.3 KB

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