1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hayrullah\Likes\Traits; |
4
|
|
|
|
5
|
|
|
use Hayrullah\LaravelLikes\Models\Like; |
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
8
|
|
|
use Illuminate\Support\Facades\Auth; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* |
12
|
|
|
* @license MIT |
13
|
|
|
* @package Hayrullah/laravel-likes |
14
|
|
|
* |
15
|
|
|
* Copyright @2020 Zaher Khirullah |
16
|
|
|
*/ |
17
|
|
|
trait Likable |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Add deleted observer to delete likes registers |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public static function bootLikable() |
25
|
|
|
{ |
26
|
|
|
static::deleted(function ($model) { |
27
|
|
|
$model->likes()->delete(); |
28
|
|
|
}); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Toggle the like status from this Object |
33
|
|
|
* |
34
|
|
|
* @param int $user_id |
35
|
|
|
*/ |
36
|
|
|
public function toggleLike($user_id = null) |
37
|
|
|
{ |
38
|
|
|
$this->isLiked($user_id) ? $this->removeLike($user_id) : $this->addLike($user_id); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Check if the user has liked this Object |
43
|
|
|
* |
44
|
|
|
* @param int $user_id |
45
|
|
|
* |
46
|
|
|
* @return boolean |
47
|
|
|
*/ |
48
|
|
|
public function isLiked($user_id = null) |
49
|
|
|
{ |
50
|
|
|
return $this->likes()->where('user_id', $this->getUserId($user_id))->exists(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Define a polymorphic one-to-many relationship. |
55
|
|
|
* |
56
|
|
|
* @return MorphMany |
57
|
|
|
*/ |
58
|
|
|
public function likes() |
59
|
|
|
{ |
60
|
|
|
return $this->morphMany(Like::class, 'likable'); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param null $user_id (if null its added to the auth user) |
|
|
|
|
65
|
|
|
* |
66
|
|
|
* @return null |
67
|
|
|
*/ |
68
|
|
|
private function getUserId($user_id = null) |
69
|
|
|
{ |
70
|
|
|
$user_id = ($user_id) ? $user_id : null; |
|
|
|
|
71
|
|
|
if (!$user_id) { |
|
|
|
|
72
|
|
|
\auth()->check() ? Auth::id() : null; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $user_id; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Remove this Object from the user likes |
80
|
|
|
* |
81
|
|
|
* @param int $user_id [if null its added to the auth user] |
82
|
|
|
* |
83
|
|
|
*/ |
84
|
|
|
public function removeLike($user_id = null) |
85
|
|
|
{ |
86
|
|
|
$this->likes()->where('user_id', $this->getUserId($user_id))->delete(); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add this Object to the user likes |
91
|
|
|
* |
92
|
|
|
* @param int $user_id |
93
|
|
|
*/ |
94
|
|
|
public function addLike($user_id = null) |
95
|
|
|
{ |
96
|
|
|
$like = new Like(['user_id' => $this->getUserId($user_id)]); |
|
|
|
|
97
|
|
|
$this->likes()->save($like); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Return a collection with the Users who marked as like this Object. |
102
|
|
|
* |
103
|
|
|
* @return Collection |
104
|
|
|
*/ |
105
|
|
|
public function likedBy() |
106
|
|
|
{ |
107
|
|
|
$likes = $this->likes()->with('user')->get(); |
108
|
|
|
|
109
|
|
|
return $likes->mapWithKeys(function ($like) { |
110
|
|
|
return [$like['user']->id => $like['user']]; |
111
|
|
|
}); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Count the number of likes |
116
|
|
|
* |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
|
|
public function getLikesCountAttribute() |
120
|
|
|
{ |
121
|
|
|
return $this->likes()->count(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return likesCount attribute |
|
|
|
|
126
|
|
|
*/ |
127
|
|
|
public function likesCount() |
128
|
|
|
{ |
129
|
|
|
return $this->likes_count; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths