Banner.php 1.2 KB

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