Question.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Relations\BelongsToCategoryTrait;
  4. use App\Models\Relations\BelongsToUserDataTrait;
  5. use App\Models\Relations\BelongsToUserTrait;
  6. use App\Models\Relations\MorphManyCommentsTrait;
  7. use App\Models\Relations\MorphManyTagsTrait;
  8. use Carbon\Carbon;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Support\Facades\App;
  11. use Illuminate\Support\Facades\Cache;
  12. class Question extends Model
  13. {
  14. use BelongsToUserTrait, MorphManyCommentsTrait, MorphManyTagsTrait, BelongsToCategoryTrait;
  15. protected $table = 'questions';
  16. protected $fillable = [
  17. 'title',
  18. 'user_id',
  19. 'category_id',
  20. 'description',
  21. 'price',
  22. 'hide',
  23. 'status',
  24. 'device',
  25. 'created_at',
  26. 'updated_at'
  27. ];
  28. public static function boot()
  29. {
  30. parent::boot();
  31. /*监听问题创建*/
  32. static::creating(function ($question) {
  33. /*开启问题审核状态检查*/
  34. if (Setting()->get('verify_question') == 1 && $question->status != -1) {
  35. $question->status = 0;
  36. }
  37. });
  38. static::saved(function ($question) {
  39. if (Setting()->get('xunsearch_open', 0) == 1) {
  40. App::offsetGet('search')->update($question);
  41. }
  42. });
  43. static::deleted(function ($question) {
  44. /*删除回答数据*/
  45. Answer::where("question_id", "=", $question->id)->delete();
  46. UserData::where("user_id", "=", $question->user_id)->where("questions", ">", 0)->decrement('questions');
  47. /*删除问题评论*/
  48. Comment::where('source_type', '=', get_class($question))->where('source_id', '=', $question->id)->delete();
  49. /*删除动态*/
  50. Doing::where('source_type', '=', get_class($question))->where('source_id', '=', $question->id)->delete();
  51. /*删除问题关注*/
  52. Attention::where('source_type', '=', get_class($question))->where('source_id', '=',
  53. $question->id)->delete();
  54. /*删除标签关联*/
  55. Taggable::where('taggable_type', '=', get_class($question))->where('taggable_id', '=',
  56. $question->id)->delete();
  57. /*删除问题邀请*/
  58. QuestionInvitation::where('question_id', '=', $question->id)->delete();
  59. /*删除问题收藏*/
  60. Collection::where('source_type', '=', get_class($question))->where('source_id', '=',
  61. $question->id)->delete();
  62. if (Setting()->get('xunsearch_open', 0) == 1) {
  63. App::offsetGet('search')->delete($question);
  64. }
  65. });
  66. }
  67. public function getImagesAttribute($value)
  68. {
  69. if ($value) {
  70. return explode(",", $value);
  71. }
  72. return [];
  73. }
  74. /*获取相关问题*/
  75. public static function correlations($tagIds, $size = 6)
  76. {
  77. $questions = self::whereHas('tags', function ($query) use ($tagIds) {
  78. $query->whereIn('tag_id', $tagIds);
  79. })->orderBy('created_at', 'DESC')->take($size)->get();
  80. return $questions;
  81. }
  82. /*热门问题*/
  83. public static function hottest($categoryId = 0, $pageSize = 20)
  84. {
  85. $query = self::with('user');
  86. $category = Category::findFromCache($categoryId);
  87. if ($category) {
  88. $query->whereIn('category_id', $category->getSubIds());
  89. }
  90. if(Setting()->get('hot_content_period',365)){
  91. $query->where('created_at', ">" , Carbon::now()->subDays(Setting()->get('hot_content_period',365)));
  92. }
  93. $list = $query->where('status', '>', 0)->orderBy('views', 'DESC')->orderBy('answers',
  94. 'DESC')->orderBy('created_at', 'DESC')->paginate($pageSize);
  95. return $list;
  96. }
  97. /*最新问题*/
  98. public static function newest($categoryId = 0, $pageSize = 20)
  99. {
  100. $query = self::with('user');
  101. $category = Category::findFromCache($categoryId);
  102. if ($category) {
  103. $query->whereIn('category_id', $category->getSubIds());
  104. }
  105. $list = $query->where('status', '>', 0)->orderBy('created_at', 'DESC')->paginate($pageSize);
  106. return $list;
  107. }
  108. /*未回答的*/
  109. public static function unAnswered($categoryId = 0, $pageSize = 20)
  110. {
  111. $query = self::query();
  112. $category = Category::findFromCache($categoryId);
  113. if ($category) {
  114. $query->whereIn('category_id', $category->getSubIds());
  115. }
  116. $list = $query->where('status', '>', 0)->where('answers', '=', 0)->orderBy('created_at',
  117. 'DESC')->paginate($pageSize);
  118. return $list;
  119. }
  120. /*悬赏问题*/
  121. public static function reward($categoryId = 0, $pageSize = 20)
  122. {
  123. $query = self::query();
  124. $category = Category::findFromCache($categoryId);
  125. if ($category) {
  126. $query->whereIn('category_id', $category->getSubIds());
  127. }
  128. $list = $query->where('status', '>', 0)->where('price', '>', 0)->orderBy('created_at',
  129. 'DESC')->paginate($pageSize);
  130. return $list;
  131. }
  132. /*最近热门问题*/
  133. public static function recent()
  134. {
  135. $list = Cache::remember('recent_questions', 300, function () {
  136. return self::where('status', '>', 0)->where('created_at', '>', Carbon::today()->subWeek())->orderBy('views',
  137. 'DESC')->orderBy('answers', 'DESC')->orderBy('created_at', 'DESC')->take(12)->get();
  138. });
  139. return $list;
  140. }
  141. /*是否已经邀请用户回答了*/
  142. public function isInvited($sendTo, $fromUserId)
  143. {
  144. return $this->invitations()->where("send_to", "=", $sendTo)->where("from_user_id", "=", $fromUserId)->count();
  145. }
  146. /*问题搜索*/
  147. public static function search($word, $size = 16)
  148. {
  149. $query = self::query();
  150. foreach (explode(" ", $word) as $item) {
  151. $query->orWhere('title', 'like', "%$item%");
  152. $query->orWhere('description', 'like', "%$item%");
  153. }
  154. $list = $query->paginate($size);
  155. return $list;
  156. }
  157. /*问题所有回答*/
  158. public function answers()
  159. {
  160. return $this->hasMany('App\Models\Answer', 'question_id');
  161. }
  162. /*问题所有邀请*/
  163. public function invitations()
  164. {
  165. return $this->hasMany('App\Models\QuestionInvitation', 'question_id');
  166. }
  167. }