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

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