Banner.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Repositories\Models\Base;
  3. use App\Repositories\Models\Model;
  4. use Carbon\Carbon;
  5. class Banner extends Model
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $table = 'base_banners';
  11. protected $guarded = [];
  12. protected $casts = [
  13. 'body' => 'json'
  14. ];
  15. /**
  16. * The attributes excluded from the model's JSON form.
  17. *
  18. * @var array
  19. */
  20. protected $hidden = [];
  21. protected static function booted()
  22. {
  23. parent::booted(); // TODO: Change the autogenerated stub
  24. self::saving(function (Banner $banner) {
  25. if (empty($banner->start_time)) {
  26. $banner->start_time = Carbon::now()->toDateTimeString();
  27. }
  28. });
  29. }
  30. public function getUrlAttribute()
  31. {
  32. if (array_key_exists('cover', $this->attributes)) return path_to_url($this->attributes['cover']);
  33. return '';
  34. }
  35. public function scopeActive($query)
  36. {
  37. $now = Carbon::now();
  38. return $query->where('start_time', '<=', $now)->where('end_time', '>=', $now);
  39. }
  40. }