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

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