-
Notifications
You must be signed in to change notification settings - Fork 3
/
isFavorer.php
58 lines (50 loc) · 1.38 KB
/
isFavorer.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\Favourite;
use Illuminate\Database\Eloquent\Model;
use Pharaonic\Laravel\Users\Models\Actions\Favourite;
trait isFavorer
{
/**
* Favourite Model
*
* @param Model $favourable
* @return boolean
*/
public function favourite(Model $favourable)
{
if ($this->favoured($favourable))
return true;
return $this->favourites()->make()->favourable()->associate($favourable)->save();
}
/**
* Unfavourite Model
*
* @param Model $favourable
* @return boolean
*/
public function unfavourite(Model $favourable)
{
if ($favourite = $this->favourites()->where(['favourable_id' => $favourable->getKey(), 'favourable_type' => get_class($favourable)])->first())
return $favourite->delete();
return false;
}
/**
* Has Favoured By Favour
*
* @param Model $favourable
* @return boolean
*/
public function favoured(Model $favourable)
{
return $this->favourites()->where(['favourable_id' => $favourable->getKey(), 'favourable_type' => get_class($favourable)])->exists();
}
/**
* Favourites List
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function favourites()
{
return $this->morphMany(Favourite::class, 'favorer');
}
}