2
0

ProgressChapter.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. 'channel_id',
  12. 'lang',
  13. 'all_trans',
  14. 'public',
  15. 'progress',
  16. 'title',
  17. 'created_at',
  18. 'updated_at'
  19. ];
  20. protected $casts = [
  21. 'uid' => 'string'
  22. ];
  23. protected $primaryKey = 'uid';
  24. //protected $dateFormat = 'U';
  25. public function tagid()
  26. {
  27. return $this->hasOne('App\Models\TagMap', 'anchor_id', 'uid'); //参数一:需要关联的子表类名,前面必须加上命名空间 参数二:子表关联父表的字段 参数三:父表关联子表的字段
  28. }
  29. public function tags()
  30. {
  31. return $this->belongsToMany(
  32. 'App\Models\Tag',
  33. 'tag_maps',
  34. 'anchor_id',
  35. 'tag_id',
  36. 'uid'
  37. );
  38. }
  39. /**
  40. * 关联到 Channel 模型
  41. * channel_id 关联到 channel 表的 uid 字段
  42. */
  43. public function channel()
  44. {
  45. return $this->hasOne(Channel::class, 'uid', 'channel_id');
  46. }
  47. public function paliText()
  48. {
  49. return $this->hasOne(PaliText::class, 'book', 'book')
  50. ->whereColumn('paragraph', 'progress_chapters.para');
  51. }
  52. public function views()
  53. {
  54. return $this->hasMany('App\Models\View', 'target_id', 'uid');
  55. }
  56. // 访问器格式化 created_at 字段
  57. public function getFormattedCreateAtAttribute($value)
  58. {
  59. return Carbon::parse($value)->format('Y-m-d H:i:s');
  60. // 你也可以使用其他格式:format('d/m/Y'), format('Y年m月d日') 等
  61. }
  62. public function getFormattedUpdatedAtAttribute()
  63. {
  64. //return Carbon::parse($value)->format('Y-m-d H:i:s');
  65. return $this->updated_at->format('Y年m月d日 H:i');
  66. // 你也可以使用其他格式:format('d/m/Y'), format('Y年m月d日') 等
  67. }
  68. }