-
Notifications
You must be signed in to change notification settings - Fork 3
/
isLiker.php
58 lines (50 loc) · 1.27 KB
/
isLiker.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
<?php
namespace Pharaonic\Laravel\Users\Traits\Actions\Like;
use Illuminate\Database\Eloquent\Model;
use Pharaonic\Laravel\Users\Models\Actions\Like;
trait isLiker
{
/**
* Like Model
*
* @param Model $likeable
* @return boolean
*/
public function like(Model $likeable)
{
if ($this->liked($likeable))
return true;
return $this->likes()->make()->likeable()->associate($likeable)->save();
}
/**
* Unlike Model
*
* @param Model $likeable
* @return boolean
*/
public function unlike(Model $likeable)
{
if ($like = $this->likes()->where(['likeable_id' => $likeable->getKey(), 'likeable_type' => get_class($likeable)])->first())
return $like->delete();
return false;
}
/**
* Has Liked By Liker
*
* @param Model $likeable
* @return boolean
*/
public function liked(Model $likeable)
{
return $this->likes()->where(['likeable_id' => $likeable->getKey(), 'likeable_type' => get_class($likeable)])->exists();
}
/**
* liker List
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function likes()
{
return $this->morphMany(Like::class, 'liker');
}
}