PostModel.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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'];
  23. public function getTimeTextAttribute()
  24. {
  25. return $this->create_time? datetime($this->create_time) : '';
  26. }
  27. public function getCoverAttribute($value)
  28. {
  29. return $value? get_image_url($value) : '';
  30. }
  31. public function setCoverAttribute($value)
  32. {
  33. return $value? get_image_path($value) : '';
  34. }
  35. public function getTagsAttribute($value)
  36. {
  37. $tags = $value? explode(',',$value) : [];
  38. return array_filter($tags);
  39. }
  40. public function getAlbumsAttribute($value)
  41. {
  42. return $value? json_decode($value,true) : [];
  43. }
  44. public function setAlbumsAttribute($value)
  45. {
  46. return $value? json_encode($value,256) : [];
  47. }
  48. public function setVideosAttribute($value)
  49. {
  50. return $value? json_encode($value,256) : [];
  51. }
  52. public function getVideosAttribute($value)
  53. {
  54. return $value? json_decode($value,true) : [];
  55. }
  56. /**
  57. * 发布用户
  58. * @return \Illuminate\Database\Eloquent\Relations\HasOne
  59. */
  60. public function user()
  61. {
  62. return $this->hasOne(MemberModel::class, 'id', 'user_id')
  63. ->select(['id','openid', 'mobile', 'nickname','balance', 'realname', 'status']);
  64. }
  65. /**
  66. * 评论记录
  67. * @return \Illuminate\Database\Eloquent\Relations\hasMany
  68. */
  69. public function records()
  70. {
  71. return $this->hasMany(PostRecordModel::class, 'source_id', 'id')
  72. ->where(['type'=>1,'status'=>1,'mark'=>1]);
  73. }
  74. /**
  75. * 点赞记录
  76. * @return \Illuminate\Database\Eloquent\Relations\hasMany
  77. */
  78. public function likes()
  79. {
  80. return $this->hasMany(PostRecordModel::class, 'source_id', 'id')
  81. ->where(['type'=>2,'status'=>1,'mark'=>1]);
  82. }
  83. /**
  84. * 是否点赞
  85. * @return \Illuminate\Database\Eloquent\Relations\hasMany
  86. */
  87. public function isLike()
  88. {
  89. return $this->hasOne(PostRecordModel::class, 'source_id', 'id')
  90. ->where(['type'=>2,'status'=>1,'mark'=>1]);
  91. }
  92. }