GeneralLog.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Events\QueryExecuted;
  5. use Illuminate\Http\Request;
  6. class GeneralLog extends Model
  7. {
  8. protected $table = 'tb_general_log';
  9. protected $primaryKey = 'id';
  10. public $keyType = 'int';
  11. public $incrementing = false;
  12. public $timestamps = false;
  13. const CREATED_AT = 'created_at';
  14. const UPDATED_AT = null;
  15. const DELETED_AT = null;
  16. protected $guarded = [];
  17. public function user()
  18. {
  19. return $this->belongsTo(User::class)->withDefault();
  20. }
  21. /**
  22. * DB SQL 처리 등록
  23. */
  24. public function register(QueryExecuted $query, Request $request): bool
  25. {
  26. $request->offsetUnset('_token');
  27. $gets = $request->getQueryString();
  28. if ($gets) {
  29. parse_str($gets, $arr);
  30. if (array_key_exists('_token', $arr)) {
  31. unset($arr['_token']);
  32. }
  33. $gets = http_build_query($arr);
  34. }
  35. $posts = json_encode($request->toArray());
  36. if($posts === '[]') {
  37. $posts = null;
  38. }
  39. return $this->insert([
  40. 'user_id' => $request->user()?->id,
  41. 'method' => $request->getMethod(),
  42. 'url' => FULL_URL,
  43. 'gets' => $gets,
  44. 'posts' => $posts,
  45. 'sql' => $query->sql,
  46. 'bindings' => implode(", ", $query->bindings),
  47. 'time' => $query->time,
  48. 'referrer' => REFERER,
  49. 'ip_address' => IP_ADDRESS,
  50. 'user_agent' => USER_AGENT,
  51. 'created_at' => now()
  52. ]);
  53. }
  54. }