usamamuneerchaudhary /
commentify
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Usamamuneerchaudhary\Commentify\Scopes; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Relations\HasMany; |
||
| 6 | use Usamamuneerchaudhary\Commentify\Models\CommentLike; |
||
| 7 | use Usamamuneerchaudhary\Commentify\Models\User; |
||
| 8 | |||
| 9 | trait HasLikes |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @return HasMany |
||
| 13 | */ |
||
| 14 | public function likes(): HasMany |
||
| 15 | { |
||
| 16 | return $this->hasMany(CommentLike::class); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @return bool |
||
| 21 | */ |
||
| 22 | public function isLiked(): bool |
||
| 23 | { |
||
| 24 | $ip = request()->ip(); |
||
| 25 | $userAgent = request()->userAgent(); |
||
| 26 | if (auth()->user()) { |
||
| 27 | return $this->likes()->where('user_id', auth()->user()->id)->exists(); |
||
| 28 | } |
||
| 29 | |||
| 30 | if ($ip && $userAgent) { |
||
| 31 | return $this->likes()->forIp($ip)->forUserAgent($userAgent)->exists(); |
||
| 32 | } |
||
| 33 | |||
| 34 | return false; |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return bool |
||
| 39 | */ |
||
| 40 | public function removeLike(): bool |
||
| 41 | { |
||
| 42 | $ip = request()->ip(); |
||
| 43 | $userAgent = request()->userAgent(); |
||
| 44 | if (auth()->user()) { |
||
| 45 | return $this->likes()->where('user_id', auth()->user()->id)->where('comment_id', $this->id)->delete(); |
||
| 46 | } |
||
| 47 | |||
| 48 | if ($ip && $userAgent) { |
||
| 49 | return $this->likes()->forIp($ip)->forUserAgent($userAgent)->delete(); |
||
| 50 | } |
||
| 51 | |||
| 52 | return false; |
||
| 53 | } |
||
| 54 | } |
||
| 55 |