-
Notifications
You must be signed in to change notification settings - Fork 151
/
Friendship.php
136 lines (115 loc) · 3.81 KB
/
Friendship.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Hootlex\Friendships\Models;
use Hootlex\Friendships\Status;
use Illuminate\Database\Eloquent\Model;
/**
* Class Friendship
* @package Hootlex\Friendships\Models
*/
class Friendship extends Model
{
/**
* @var array
*/
protected $guarded = ['id', 'created_at', 'updated_at'];
/**
* @param array $attributes
*/
public function __construct(array $attributes = array())
{
$this->table = config('friendships.tables.fr_pivot');
parent::__construct($attributes);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function sender()
{
return $this->morphTo('sender');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function recipient()
{
return $this->morphTo('recipient');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\hasMany
*/
public function groups() {
return $this->hasMany(FriendFriendshipGroups::class, 'friendship_id');
}
/**
* @param Model $recipient
* @return $this
*/
public function fillRecipient($recipient)
{
return $this->fill([
'recipient_id' => $recipient->getKey(),
'recipient_type' => $recipient->getMorphClass()
]);
}
/**
* @param $query
* @param Model $model
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeWhereRecipient($query, $model)
{
return $query->where('recipient_id', $model->getKey())
->where('recipient_type', $model->getMorphClass());
}
/**
* @param $query
* @param Model $model
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeWhereSender($query, $model)
{
return $query->where('sender_id', $model->getKey())
->where('sender_type', $model->getMorphClass());
}
/**
* @param $query
* @param Model $model
* @param string $groupSlug
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeWhereGroup($query, $model, $groupSlug)
{
$groupsPivotTable = config('friendships.tables.fr_groups_pivot');
$friendsPivotTable = config('friendships.tables.fr_pivot');
$groupsAvailable = config('friendships.groups', []);
if ('' !== $groupSlug && isset($groupsAvailable[$groupSlug])) {
$groupId = $groupsAvailable[$groupSlug];
$query->join($groupsPivotTable, function ($join) use ($groupsPivotTable, $friendsPivotTable, $groupId, $model) {
$join->on($groupsPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id')
->where($groupsPivotTable . '.group_id', '=', $groupId)
->where(function ($query) use ($groupsPivotTable, $friendsPivotTable, $model) {
$query->where($groupsPivotTable . '.friend_id', '!=', $model->getKey())
->where($groupsPivotTable . '.friend_type', '=', $model->getMorphClass());
})
->orWhere($groupsPivotTable . '.friend_type', '!=', $model->getMorphClass());
});
}
return $query;
}
/**
* @param $query
* @param Model $sender
* @param Model $recipient
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeBetweenModels($query, $sender, $recipient)
{
$query->where(function ($queryIn) use ($sender, $recipient){
$queryIn->where(function ($q) use ($sender, $recipient) {
$q->whereSender($sender)->whereRecipient($recipient);
})->orWhere(function ($q) use ($sender, $recipient) {
$q->whereSender($recipient)->whereRecipient($sender);
});
});
}
}