// +---------------------------------------------------------------------- namespace App\Models; /** * 笔记动态-模型 * @author laravel开发员 * @since 2020/11/11 * @package App\Models */ class PostModel extends BaseModel { // 设置数据表 protected $table = 'posts'; protected $appends = ['time_text','publish_at']; public function getTimeTextAttribute() { return $this->create_time? dateFormat($this->create_time,'Y年m月d日') : ''; } public function getPublishAtAttribute() { return $this->create_time? dateFormat($this->create_time,'Y-m-d H:i') : ''; } public function getCoverAttribute($value) { return $value? get_image_url($value) : ''; } public function getVideoUrlAttribute($value) { return $value? get_image_url($value) : ''; } public function setCoverAttribute($value) { return $value? get_image_path($value) : ''; } public function getTagsAttribute($value) { $tags = $value? explode(',',$value) : []; return array_filter($tags); } public function getAlbumsAttribute($value) { return $value? get_images_preview($value,'url',2) : []; } public function setAlbumsAttribute($value) { return $value? json_encode($value,256) : []; } public function setVideosAttribute($value) { return $value? json_encode($value,256) : []; } public function getVideosAttribute($value) { return $value? json_decode($value,true) : []; } /** * 发布用户 * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function user() { return $this->hasOne(MemberModel::class, 'id', 'user_id') ->select(['id','openid', 'mobile','avatar', 'nickname','balance', 'realname', 'status']); } /** * 评论记录 * @return \Illuminate\Database\Eloquent\Relations\hasMany */ public function records() { return $this->hasMany(PostRecordModel::class, 'source_id', 'id') ->where(['type'=>1,'status'=>1,'mark'=>1]); } /** * 点赞记录 * @return \Illuminate\Database\Eloquent\Relations\hasMany */ public function likes() { return $this->hasMany(PostRecordModel::class, 'source_id', 'id') ->where(['type'=>2,'status'=>1,'mark'=>1]); } /** * 是否点赞 * @return \Illuminate\Database\Eloquent\Relations\hasMany */ public function isLike() { return $this->hasOne(PostRecordModel::class, 'source_id', 'id') ->where(['type'=>2,'status'=>1,'mark'=>1]) ->select(['id','type','user_id','source_id','status']); } }