ProgressChapter.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. 'para',
  12. 'channel_id',
  13. 'lang',
  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 paliText()
  49. {
  50. return $this->hasOne(PaliText::class, 'book', 'book')
  51. ->whereColumn('paragraph', 'progress_chapters.para');
  52. }
  53. public function views()
  54. {
  55. return $this->hasMany('App\Models\View', 'target_id', 'uid');
  56. }
  57. // 访问器格式化 created_at 字段
  58. public function getFormattedCreateAtAttribute($value)
  59. {
  60. return Carbon::parse($value)->format('Y-m-d H:i:s');
  61. // 你也可以使用其他格式:format('d/m/Y'), format('Y年m月d日') 等
  62. }
  63. public function getFormattedUpdatedAtAttribute()
  64. {
  65. //return Carbon::parse($value)->format('Y-m-d H:i:s');
  66. return $this->updated_at->format('Y年m月d日 H:i');
  67. // 你也可以使用其他格式:format('d/m/Y'), format('Y年m月d日') 等
  68. }
  69. }