ProgressChapter.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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(
  33. 'App\Models\Tag',
  34. 'tag_maps',
  35. 'anchor_id',
  36. 'tag_id',
  37. 'uid'
  38. );
  39. }
  40. /**
  41. * 关联到 Channel 模型
  42. * channel_id 关联到 channel 表的 uid 字段
  43. */
  44. public function channel()
  45. {
  46. return $this->hasOne(Channel::class, 'uid', 'channel_id');
  47. }
  48. public function views()
  49. {
  50. return $this->hasMany('App\Models\View', 'target_id', 'uid');
  51. }
  52. // 访问器格式化 created_at 字段
  53. public function getFormattedCreateAtAttribute($value)
  54. {
  55. return Carbon::parse($value)->format('Y-m-d H:i:s');
  56. // 你也可以使用其他格式:format('d/m/Y'), format('Y年m月d日') 等
  57. }
  58. public function getFormattedUpdatedAtAttribute()
  59. {
  60. //return Carbon::parse($value)->format('Y-m-d H:i:s');
  61. return $this->updated_at->format('Y年m月d日 H:i');
  62. // 你也可以使用其他格式:format('d/m/Y'), format('Y年m月d日') 等
  63. }
  64. }