ProgressChapter.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Carbon\Carbon;
  6. class ProgressChapter extends Model
  7. {
  8. use HasFactory;
  9. protected $fillable = [
  10. 'book',
  11. 'book',
  12. 'channel_id',
  13. 'lang' => 'en',
  14. 'all_trans',
  15. 'public',
  16. 'progress',
  17. 'title',
  18. 'created_at',
  19. 'updated_at'
  20. ];
  21. protected $casts = [
  22. 'uid' => 'string'
  23. ];
  24. protected $primaryKey = 'uid';
  25. //protected $dateFormat = 'U';
  26. public function tagid()
  27. {
  28. return $this->hasOne('App\Models\TagMap', 'anchor_id', 'uid'); //参数一:需要关联的子表类名,前面必须加上命名空间 参数二:子表关联父表的字段 参数三:父表关联子表的字段
  29. }
  30. public function tags()
  31. {
  32. return $this->belongsToMany('App\Models\Tag', 'tag_maps', 'anchor_id', 'tag_id');
  33. }
  34. /**
  35. * 关联到 Channel 模型
  36. * channel_id 关联到 channel 表的 uid 字段
  37. */
  38. public function channel()
  39. {
  40. return $this->hasOne(Channel::class, 'uid', 'channel_id');
  41. }
  42. public function views()
  43. {
  44. return $this->hasMany('App\Models\View', 'target_id', 'uid');
  45. }
  46. // 访问器格式化 created_at 字段
  47. public function getFormattedCreateAtAttribute($value)
  48. {
  49. return Carbon::parse($value)->format('Y-m-d H:i:s');
  50. // 你也可以使用其他格式:format('d/m/Y'), format('Y年m月d日') 等
  51. }
  52. public function getFormattedUpdatedAtAttribute()
  53. {
  54. //return Carbon::parse($value)->format('Y-m-d H:i:s');
  55. return $this->updated_at->format('Y年m月d日 H:i');
  56. // 你也可以使用其他格式:format('d/m/Y'), format('Y年m月d日') 等
  57. }
  58. }