This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
User.php
167 lines (144 loc) · 3.71 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
namespace Conduit\Models;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer id
* @property string email
* @property string username
* @property string image
* @property string bio
* @property string token
* @property string password
* @property \Carbon\Carbon created_at
* @property \Carbon\Carbon update_at
* @property \Illuminate\Database\Eloquent\Collection followings Users who are followed by this user
*/
class User extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username',
'email',
'password',
'token',
'image',
'bio',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
];
/**
* Return Default Image Profile When User Does Not Have An Image
*
* @param $value
*
* @return string
*/
public function getImageAttribute($value)
{
if (is_null($value)) {
return 'https://static.productionready.io/images/smiley-cyrus.jpg';
}
return $value;
}
/********************
* Relationships
********************/
public function comments()
{
return $this->hasMany(Comment::class);
}
public function favoriteArticles()
{
return $this->belongsToMany(Article::class, 'user_favorite');
}
/**
* Create Many-To-Many following relationship
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function followings()
{
return $this->belongsToMany(
User::class,
'users_following',
'user_id',
'following_user_id')
->withTimestamps();
}
/**
* Create following relationship. This user will follow the user with the provided id
*
* @param $id
*
* @return self
*/
public function follow($id)
{
$this->followings()->syncWithoutDetaching([$id]);
return $this;
}
/**
* Remove following relationship. This user will unfollow the user with the provided id
*
* @param $id
*
* @return self
*/
public function unFollow($id)
{
$this->followings()->detach($id);
return $this;
}
/**
* Check if this user is following the user with the provided id
*
* @param $id
*
* @return bool|\Conduit\Models\User
*/
public function isFollowing($id = null)
{
if (is_null($id)) {
return false;
}
if ($id instanceof self) {
$id = $id->id;
}
return $this->newBaseQueryBuilder()
->from('users_following')
->where('user_id', $this->id)
->where('following_user_id', $id)
->exists();
}
/**
* Check if this user is followed by the user with the provided id
*
* @param $id
*
* @return bool|\Conduit\Models\User
*/
public function isFollowedBy($id = null)
{
if (is_null($id)) {
return false;
}
if ($id instanceof self) {
$id = $id->id;
}
return $this->newBaseQueryBuilder()
->from('users_following')
->where('user_id', $id)
->where('following_user_id', $this->id)
->exists();
}
}