Banner.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace App\Repositories\Models\CMS;
  3. use App\Repositories\Models\Base\Resource;
  4. use App\Repositories\Models\Model;
  5. use Carbon\Carbon;
  6. use Prettus\Repository\Contracts\Transformable;
  7. use Prettus\Repository\Traits\TransformableTrait;
  8. /**
  9. * Class Banner.
  10. *
  11. * @package namespace App\Repositories\Models\CMS;
  12. */
  13. class Banner extends Model implements Transformable
  14. {
  15. use TransformableTrait;
  16. /**
  17. * The attributes that are mass assignable.
  18. *
  19. * @var array
  20. */
  21. protected $table = 'cms_banners';
  22. /**
  23. * The attributes that are mass assignable.
  24. *
  25. * @var array
  26. */
  27. protected $guarded = [];
  28. protected $casts = ['click_body' => 'array'];
  29. protected static function booted()
  30. {
  31. self::language();
  32. static::saving(function (Banner $banner) {
  33. if (empty($banner->start_time)) {
  34. $banner->start_time = Carbon::now()->toDateTimeString();
  35. }
  36. });
  37. }
  38. public function cover_resource()
  39. {
  40. return $this->belongsTo(Resource::class, 'cover', 'id')->select(['id', 'url', 'path']);
  41. }
  42. public function scopeActive($query)
  43. {
  44. $now = Carbon::now();
  45. return $query->where('start_time', '<=', $now)->where('end_time', '>=', $now);
  46. }
  47. }