Comment.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Relations\BelongsToUserTrait;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Comment extends Model
  6. {
  7. use BelongsToUserTrait;
  8. protected $table = 'comments';
  9. protected $fillable = ['user_id', 'content','source_id','source_type','to_user_id','supports','device','status'];
  10. public static function boot()
  11. {
  12. parent::boot();
  13. /*监听创建*/
  14. static::creating(function($comment){
  15. /*开启状态检查*/
  16. if(Setting()->get('verify_comment')==1){
  17. $comment->status = 0;
  18. }
  19. });
  20. /*监听删除事件*/
  21. static::deleting(function($comment){
  22. /*问题、回答、文章评论数 -1*/
  23. $comment->source()->where("comments",">",0)->decrement('comments');
  24. });
  25. }
  26. public function source()
  27. {
  28. return $this->morphTo();
  29. }
  30. public function toUser(){
  31. return $this->belongsTo('App\Models\User','to_user_id');
  32. }
  33. }