Просмотр файла app/Models/Reader.php

Размер файла: 1.15Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. /**
  8. * Class Reader
  9. *
  10. * @property int id
  11. * @property string relate_type
  12. * @property int relate_id
  13. * @property string ip
  14. * @property int created_at
  15. */
  16. class Reader extends BaseModel
  17. {
  18. /**
  19. * Indicates if the model should be timestamped.
  20. *
  21. * @var bool
  22. */
  23. public $timestamps = false;
  24.  
  25. /**
  26. * The attributes that aren't mass assignable.
  27. *
  28. * @var array
  29. */
  30. protected $guarded = [];
  31.  
  32. /**
  33. * Counting stat
  34. *
  35. * @param BaseModel $model
  36. */
  37. public static function countingStat(BaseModel $model): void
  38. {
  39. $reader = self::query()
  40. ->where('relate_type', $model->getMorphClass())
  41. ->where('relate_id', $model->id)
  42. ->where('ip', getIp())
  43. ->first();
  44.  
  45. if (! $reader) {
  46. self::query()->create([
  47. 'relate_type' => $model->getMorphClass(),
  48. 'relate_id' => $model->id,
  49. 'ip' => getIp(),
  50. 'created_at' => SITETIME,
  51. ]);
  52.  
  53. $model->increment($model->countingField);
  54. }
  55. }
  56. }