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

Размер файла: 1.68Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use Illuminate\Database\Eloquent\Collection;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\Relations\HasMany;
  10. use Illuminate\Database\Eloquent\Relations\HasOne;
  11.  
  12. /**
  13. * Class Load
  14. *
  15. * @property int id
  16. * @property int sort
  17. * @property int parent_id
  18. * @property string name
  19. * @property int count_downs
  20. * @property int closed
  21. * @property Load parent
  22. * @property Collection children
  23. */
  24. class Load extends BaseModel
  25. {
  26. /**
  27. * Indicates if the model should be timestamped.
  28. *
  29. * @var bool
  30. */
  31. public $timestamps = false;
  32.  
  33. /**
  34. * The attributes that aren't mass assignable.
  35. *
  36. * @var array
  37. */
  38. protected $guarded = [];
  39.  
  40. /**
  41. * Возвращает связь родительской категории
  42. *
  43. * @return BelongsTo
  44. */
  45. public function parent(): BelongsTo
  46. {
  47. return $this->belongsTo(self::class, 'parent_id')->withDefault();
  48. }
  49.  
  50. /**
  51. * Возвращает связь подкатегорий
  52. *
  53. * @return HasMany
  54. */
  55. public function children(): HasMany
  56. {
  57. return $this->hasMany(self::class, 'parent_id')->orderBy('sort');
  58. }
  59.  
  60. /**
  61. * Возвращает количество загрузок за последние 3 дней
  62. *
  63. * @return hasOne
  64. */
  65. public function new(): hasOne
  66. {
  67. return $this->hasOne(Down::class, 'category_id')
  68. ->selectRaw('category_id, count(*) as count_downs')
  69. ->where('active', 1)
  70. ->where('created_at', '>', strtotime('-3 day', SITETIME))
  71. ->groupBy('category_id');
  72. }
  73. }