NewMediaCount.php 945 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. class NewMediaCount extends Model
  5. {
  6. //
  7. protected $table = 'new_media_count';
  8. protected $fillable = ['day','year','month'];
  9. protected $appends = ['month','year', 'count_data'];
  10. protected $casts = [
  11. 'count_data' => 'array',
  12. ];
  13. // protected $guarded = ['year','month'];
  14. protected static function booting()
  15. {
  16. static::creating(function ($model) {
  17. $model->day = $model->year . '-' . $model->month ;
  18. unset($model->year, $model->month);
  19. });
  20. }
  21. protected function getMonthAttribute()
  22. {
  23. return date('m', strtotime($this->day));
  24. }
  25. protected function getYearAttribute()
  26. {
  27. return date('Y', strtotime($this->day));
  28. }
  29. protected function getCountDataAttribute()
  30. {
  31. return @NewMediaCountRecord::where('nmc_id', $this->id)->get()->toArray();
  32. }
  33. }