1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hayrullah\Likes\Traits; |
4
|
|
|
|
5
|
|
|
use Hayrullah\Likes\Models\Like; |
6
|
|
|
use Illuminate\Database\Eloquent\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
* @license MIT |
12
|
|
|
* @package Hayrullah/laravel-likes |
13
|
|
|
* |
14
|
|
|
* Copyright @2020 Zaher Khirullah |
15
|
|
|
*/ |
16
|
|
|
trait Likability |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Return a collection with the User likes Model. |
20
|
|
|
* The Model needs to have the likablet. |
21
|
|
|
* |
22
|
|
|
* @param $class *** Accepts for example: Post::class or 'App\Post' **** |
23
|
|
|
* |
24
|
|
|
* @return Collection |
25
|
|
|
*/ |
26
|
|
|
public function like($class) |
27
|
|
|
{ |
28
|
|
|
$likes = $this->likes()->where('likable_type', $class)->with('likable')->get(); |
29
|
|
|
|
30
|
|
|
return $likes->mapWithKeys(function ($like) { |
31
|
|
|
if (!isset($like['likable'])) { |
32
|
|
|
return []; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return [$like['likable']->id => $like['likable']]; |
36
|
|
|
}); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Define a one-to-many relationship. |
41
|
|
|
* |
42
|
|
|
* @return HasMany |
43
|
|
|
*/ |
44
|
|
|
public function likes() |
45
|
|
|
{ |
46
|
|
|
return $this->hasMany(Like::class, 'user_id'); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Add the object to the User visits. |
51
|
|
|
* The Model needs to have the likable. |
52
|
|
|
* |
53
|
|
|
* @param object $object |
54
|
|
|
*/ |
55
|
|
|
public function addLike($object) |
56
|
|
|
{ |
57
|
|
|
$object->addLike($this->id); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Remove the Object from the user visits. |
62
|
|
|
* The Model needs to have the likable. |
63
|
|
|
* |
64
|
|
|
* @param object $object |
65
|
|
|
*/ |
66
|
|
|
public function removeLike($object) |
67
|
|
|
{ |
68
|
|
|
$object->removeLike($this->id); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Toggle the visits status from this Object from the user visits. |
73
|
|
|
* The Model needs to have the likable. |
74
|
|
|
* |
75
|
|
|
* @param object $object |
76
|
|
|
*/ |
77
|
|
|
public function toggleLike($object) |
78
|
|
|
{ |
79
|
|
|
$object->toggleLikes($this->id); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Check if the user has visits this Object |
84
|
|
|
* The Model needs to have the likable. |
85
|
|
|
* |
86
|
|
|
* @param object $object |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function isLiked($object) |
91
|
|
|
{ |
92
|
|
|
return $object->isLiked($this->id); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check if the user has visited this Object |
97
|
|
|
* The Model needs to have the likable. |
98
|
|
|
* |
99
|
|
|
* @param object $object |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function hasLiked($object) |
104
|
|
|
{ |
105
|
|
|
return $object->isLiked($this->id); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|