123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace app\common\model;
- use think\db\Query;
- use think\Model;
- /**
- * 短信验证码
- */
- class Like Extends Model
- {
- // 开启自动写入时间戳字段
- protected $autoWriteTimestamp = false;
- protected $append=['like'];
- public function like(){
- return $this->morphTo('like',[
- 'info'=>Info::class
- ]);
- }
- public function scopeUser(Query $query,User $user){
- $query->where('user_id',$user['id']);
- }
- public function user(){
- return $this->belongsTo(User::class);
- }
- public function notification(){
- return $this->morphMany(Notification::class,'notice','like');
- }
- protected static function init()
- {
- self::afterInsert(function (self $like){
- if($like->like instanceof Info){
- $like->like->setInc('like_count');
- if($like->user_id!=$like->like->user_id) {
- Notification::create([
- 'notice_type'=>'like',
- 'notice_id'=>$like->id,
- 'title'=>"{$like->user->nickname}点赞了您发布的资讯",
- 'user_id'=>$like->like->user_id,
- 'from_user_id'=>$like->user_id,
- 'object'=>$like->like->toArray()
- ]);
- }
- }
- });
- self::afterDelete(function (self $like){
- if($like->like instanceof Info){
- $like->like->setDec('like_count');
- }
- });
- }
- }
|