Invite.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Models;
  3. use Auth;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\Relations\HasOne;
  7. use Illuminate\Database\Eloquent\SoftDeletes;
  8. /**
  9. * 邀请码
  10. *
  11. * @property int $id
  12. * @property int $uid 邀请人ID
  13. * @property int $fuid 受邀人ID
  14. * @property string $code 邀请码
  15. * @property int $status 邀请码状态:0-未使用、1-已使用、2-已过期
  16. * @property \Illuminate\Support\Carbon $dateline 有效期至
  17. * @property \Illuminate\Support\Carbon $created_at 创建时间
  18. * @property \Illuminate\Support\Carbon $updated_at 最后更新时间
  19. * @property \Illuminate\Support\Carbon|null $deleted_at 删除时间
  20. * @property-read \App\Models\User|null $generator
  21. * @property-read string $status_label
  22. * @property-read \App\Models\User|null $user
  23. * @method static Builder|Invite newModelQuery()
  24. * @method static Builder|Invite newQuery()
  25. * @method static Builder|Invite onlyTrashed()
  26. * @method static Builder|Invite query()
  27. * @method static Builder|Invite uid()
  28. * @method static Builder|Invite whereCode($value)
  29. * @method static Builder|Invite whereCreatedAt($value)
  30. * @method static Builder|Invite whereDateline($value)
  31. * @method static Builder|Invite whereDeletedAt($value)
  32. * @method static Builder|Invite whereFuid($value)
  33. * @method static Builder|Invite whereId($value)
  34. * @method static Builder|Invite whereStatus($value)
  35. * @method static Builder|Invite whereUid($value)
  36. * @method static Builder|Invite whereUpdatedAt($value)
  37. * @method static Builder|Invite withTrashed()
  38. * @method static Builder|Invite withoutTrashed()
  39. * @mixin \Eloquent
  40. */
  41. class Invite extends Model {
  42. use SoftDeletes;
  43. protected $table = 'invite';
  44. protected $dates = ['dateline', 'deleted_at'];
  45. public function scopeUid($query) {
  46. return $query->whereUid(Auth::id());
  47. }
  48. public function generator(): HasOne {
  49. return $this->hasOne(User::class, 'id', 'uid');
  50. }
  51. public function user(): HasOne {
  52. return $this->hasOne(User::class, 'id', 'fuid');
  53. }
  54. public function getStatusLabelAttribute(): string {
  55. switch($this->attributes['status']){
  56. case 0:
  57. $status_label = '<span class="badge badge-success">'.trans('home.invite_code_table_status_un').'</span>';
  58. break;
  59. case 1:
  60. $status_label = '<span class="badge badge-danger">'.trans('home.invite_code_table_status_yes').'</span>';
  61. break;
  62. case 2:
  63. $status_label = '<span class="badge badge-default">'.trans('home.invite_code_table_status_expire').'</span>';
  64. break;
  65. default:
  66. $status_label = '<span class="badge badge-default"> 未知 </span>';
  67. }
  68. return $status_label;
  69. }
  70. }