Like.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\common\model;
  3. use think\db\Query;
  4. use think\Model;
  5. /**
  6. * 短信验证码
  7. */
  8. class Like Extends Model
  9. {
  10. // 开启自动写入时间戳字段
  11. protected $autoWriteTimestamp = false;
  12. protected $append=['like'];
  13. public function like(){
  14. return $this->morphTo('like',[
  15. 'info'=>Info::class
  16. ]);
  17. }
  18. public function scopeUser(Query $query,User $user){
  19. $query->where('user_id',$user['id']);
  20. }
  21. public function user(){
  22. return $this->belongsTo(User::class);
  23. }
  24. public function notification(){
  25. return $this->morphMany(Notification::class,'notice','like');
  26. }
  27. protected static function init()
  28. {
  29. self::afterInsert(function (self $like){
  30. if($like->like instanceof Info){
  31. $like->like->setInc('like_count');
  32. if($like->user_id!=$like->like->user_id) {
  33. Notification::create([
  34. 'notice_type'=>'like',
  35. 'notice_id'=>$like->id,
  36. 'title'=>"{$like->user->nickname}点赞了您发布的资讯",
  37. 'user_id'=>$like->like->user_id,
  38. 'from_user_id'=>$like->user_id,
  39. 'object'=>$like->like->toArray()
  40. ]);
  41. }
  42. }
  43. });
  44. self::afterDelete(function (self $like){
  45. if($like->like instanceof Info){
  46. $like->like->setDec('like_count');
  47. }
  48. });
  49. }
  50. }