PostModel.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Models;
  12. /**
  13. * 笔记动态-模型
  14. * @author laravel开发员
  15. * @since 2020/11/11
  16. * @package App\Models
  17. */
  18. class PostModel extends BaseModel
  19. {
  20. // 设置数据表
  21. protected $table = 'posts';
  22. protected $appends = ['time_text','publish_at'];
  23. public function getTimeTextAttribute()
  24. {
  25. return $this->create_time? dateFormat($this->create_time,'Y年m月d日') : '';
  26. }
  27. public function getPublishAtAttribute()
  28. {
  29. return $this->create_time? dateFormat($this->create_time,'Y-m-d H:i') : '';
  30. }
  31. public function getCoverAttribute($value)
  32. {
  33. return $value? get_image_url($value) : '';
  34. }
  35. public function getVideoUrlAttribute($value)
  36. {
  37. return $value? get_image_url($value) : '';
  38. }
  39. public function setCoverAttribute($value)
  40. {
  41. return $value? get_image_path($value) : '';
  42. }
  43. public function getTagsAttribute($value)
  44. {
  45. $tags = $value? explode(',',$value) : [];
  46. return array_filter($tags);
  47. }
  48. public function getAlbumsAttribute($value)
  49. {
  50. return $value? get_images_preview($value,'url',2) : [];
  51. }
  52. public function setAlbumsAttribute($value)
  53. {
  54. return $value? json_encode($value,256) : [];
  55. }
  56. public function setVideosAttribute($value)
  57. {
  58. return $value? json_encode($value,256) : [];
  59. }
  60. public function getVideosAttribute($value)
  61. {
  62. return $value? json_decode($value,true) : [];
  63. }
  64. /**
  65. * 发布用户
  66. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  67. */
  68. public function user()
  69. {
  70. return $this->hasOne(MemberModel::class, 'id', 'user_id')
  71. ->select(['id','openid', 'mobile','avatar', 'nickname','balance', 'realname', 'status']);
  72. }
  73. /**
  74. * 评论记录
  75. * @return \Illuminate\Database\Eloquent\Relations\hasMany
  76. */
  77. public function records()
  78. {
  79. return $this->hasMany(PostRecordModel::class, 'source_id', 'id')
  80. ->where(['type'=>1,'status'=>1,'mark'=>1]);
  81. }
  82. /**
  83. * 点赞记录
  84. * @return \Illuminate\Database\Eloquent\Relations\hasMany
  85. */
  86. public function likes()
  87. {
  88. return $this->hasMany(PostRecordModel::class, 'source_id', 'id')
  89. ->where(['type'=>2,'status'=>1,'mark'=>1]);
  90. }
  91. /**
  92. * 是否点赞
  93. * @return \Illuminate\Database\Eloquent\Relations\hasMany
  94. */
  95. public function isLike()
  96. {
  97. return $this->hasOne(PostRecordModel::class, 'source_id', 'id')
  98. ->where(['type'=>2,'status'=>1,'mark'=>1])
  99. ->select(['id','type','user_id','source_id','status']);
  100. }
  101. }