Authentication.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Relations\BelongsToCategoryTrait;
  4. use App\Models\Relations\BelongsToUserTrait;
  5. use Illuminate\Database\Eloquent\Model;
  6. class Authentication extends Model
  7. {
  8. use BelongsToUserTrait, BelongsToCategoryTrait;
  9. protected $table = 'authentications';
  10. protected $primaryKey = 'user_id';
  11. protected $fillable = [
  12. 'user_id',
  13. 'real_name',
  14. 'province',
  15. 'city',
  16. 'gender',
  17. 'title',
  18. 'description',
  19. 'id_card',
  20. 'id_card_image',
  21. 'skill',
  22. 'skill_image',
  23. 'status',
  24. 'category_id',
  25. 'recommend_at'
  26. ];
  27. public static function boot()
  28. {
  29. parent::boot();
  30. static::saved(function ($authentication) {
  31. if ($authentication->userData) {
  32. if ($authentication->status == 1) {
  33. $authentication->userData->authentication_status = 1;
  34. } else {
  35. $authentication->userData->authentication_status = 0;
  36. }
  37. $authentication->userData->save();
  38. }
  39. });
  40. static::deleted(function ($authentication) {
  41. UserData::where("user_id","=",$authentication->user_id)->update(['authentication_status'=>0]);
  42. });
  43. }
  44. public function userData()
  45. {
  46. return $this->belongsTo('App\Models\UserData', 'user_id', 'user_id');
  47. }
  48. /*用户统计标签*/
  49. public function userTags()
  50. {
  51. return $this->hasMany('App\Models\UserTag', 'user_id', 'user_id');
  52. }
  53. public function hotTags()
  54. {
  55. $hotTagIds = $this->userTags()->select("tag_id")->distinct()->orderBy('supports', 'desc')->orderBy('answers',
  56. 'desc')->orderBy('created_at', 'desc')->take(5)->pluck('tag_id');
  57. $tags = [];
  58. foreach ($hotTagIds as $hotTagId) {
  59. $tag = Tag::find($hotTagId);
  60. if ($tag) {
  61. $tags[] = $tag;
  62. }
  63. }
  64. return $tags;
  65. }
  66. /*推荐行家*/
  67. public static function hottest($size)
  68. {
  69. return self::leftJoin('user_data', 'user_data.user_id', '=', 'authentications.user_id')
  70. ->where('authentications.recommend_at', '>', 0)
  71. ->where('user_data.authentication_status', '=', 1)
  72. ->orderBy('user_data.answers', 'DESC')
  73. ->orderBy('user_data.articles', 'DESC')
  74. ->orderBy('authentications.recommend_at', 'DESC')
  75. ->select('authentications.user_id', 'authentications.real_name','authentications.id_card_image', 'authentications.title', 'user_data.coins',
  76. 'user_data.credits', 'user_data.followers', 'user_data.supports', 'user_data.answers',
  77. 'user_data.articles', 'user_data.authentication_status')
  78. ->take($size)->get();
  79. }
  80. public function getRecommendAtAttribute($value)
  81. {
  82. return $value > 0 ? 1 : 0;
  83. }
  84. }