-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.php
117 lines (96 loc) · 2.56 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Notifications\ResetPassword;
use Illuminate\Support\Facades\Auth;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function gravatar($size = '100')
{
$hash = md5(strtolower(trim($this->attributes['email'])));
return "http://www.gravatar.com/avatar/$hash?s=$size";
}
public static function boot()
{
parent::boot();
static::creating(function ($user) {
$user->activation_token = str_random(30);
});
}
public function sendPasswordResetNotification($token){
$this->notify(new ResetPassword($token));
}
public function feed()
{
$user_ids = Auth::user()->followings->pluck('id')->toArray();
array_push($user_ids, Auth::user()->id);
return Status::whereIn('user_id', $user_ids)
->with('user')
->orderBy('created_at', 'desc');
}
/**
* 关注(成为粉丝)
* @param $user_ids
*/
public function follow($user_ids)
{
$this->followings()->sync((array)$user_ids, false);
}
/**
* 取消关注
* @param $user_ids
*/
public function unfollow($user_ids)
{
$this->followings()->detach((array)$user_ids);
}
/**
* 一个用户有多个粉丝
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function followers()
{
return $this->belongsToMany(User::Class, 'followers', 'user_id', 'follower_id');
}
/**
* 一个粉丝可以关注多个用户
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function followings()
{
return $this->belongsToMany(User::Class, 'followers', 'follower_id', 'user_id');
}
/**
* 判断当前用户有没有关注$usr_id用户
* @param $user_id 被关注的用户id
* @return mixed
*/
public function isFollowing($user_id)
{
return $this->followings->contains($user_id);
}
/**
* 指定一个用户有多条微博
*/
public function statuses(){
return $this->hasMany(Status::class);
}
}