1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Relatable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @mixin \Illuminate\Database\Eloquent\Model |
11
|
|
|
* |
12
|
|
|
* @property \Illuminate\Support\Collection $related |
13
|
|
|
* @property \Illuminate\Support\Collection $relatables |
14
|
|
|
*/ |
15
|
|
|
trait HasRelatedContent |
16
|
|
|
{ |
17
|
|
|
/** @var \Illuminate\Support\Collection|null */ |
18
|
|
|
protected $relatableCache; |
19
|
|
|
|
20
|
|
|
public function relatables() : MorphMany |
21
|
|
|
{ |
22
|
|
|
return $this->morphMany(Relatable::class, 'source'); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Returns a Collection of all related models. The results are cached as a property on the |
27
|
|
|
* model, you reload them using the `loadRelated` method. |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Support\Collection |
30
|
|
|
*/ |
31
|
|
|
public function getRelatedAttribute() : Collection |
32
|
|
|
{ |
33
|
|
|
if ($this->relatableCache === null) { |
34
|
|
|
$this->loadRelated(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $this->relatableCache; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function loadRelated($reloadRelatables = true) : Collection |
41
|
|
|
{ |
42
|
|
|
if ($reloadRelatables) { |
43
|
|
|
$this->load('relatables'); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->relatableCache = $this->relatables |
47
|
|
|
->groupBy(function (Relatable $relatable) { |
48
|
|
|
return $this->getActualClassNameForMorph($relatable->related_type); |
|
|
|
|
49
|
|
|
}) |
50
|
|
|
->flatMap(function (Collection $typeGroup, string $type) { |
51
|
|
|
return $type::whereIn('id', $typeGroup->pluck('related_id'))->get(); |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function hasRelated() : bool |
56
|
|
|
{ |
57
|
|
|
return ! $this->related->isEmpty(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The `$item` parameter must be an Eloquent model or an ID. If you provide an ID, the model's |
62
|
|
|
* morph type must be specified as a second parameter. |
63
|
|
|
* |
64
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $item |
65
|
|
|
* @param string|null $type |
66
|
|
|
* |
67
|
|
|
* @return \Spatie\Relatable\Relatable |
68
|
|
|
*/ |
69
|
|
|
public function relate($item, string $type = '') : Relatable |
70
|
|
|
{ |
71
|
|
|
return Relatable::firstOrCreate( |
72
|
|
|
$this->getRelatableValues($item, $type) |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The `$item` parameter must be an Eloquent model or an ID. If you provide an ID, the model's |
78
|
|
|
* morph type must be specified as a second parameter. |
79
|
|
|
* |
80
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $item |
81
|
|
|
* @param string|null $type |
82
|
|
|
* |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
|
|
public function unrelate($item, string $type = '') : int |
86
|
|
|
{ |
87
|
|
|
return Relatable::where($this->getRelatableValues($item, $type))->delete(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The `$items` parameter can either contain an Eloquent collection of models, or an array |
92
|
|
|
* with the shape of [['id' => int, 'type' => string], ...]. |
93
|
|
|
* |
94
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|array $items |
95
|
|
|
* @param bool $detaching |
96
|
|
|
*/ |
97
|
|
|
public function syncRelated($items, $detaching = true) |
98
|
|
|
{ |
99
|
|
|
$items = $this->getSyncRelatedValues($items); |
100
|
|
|
|
101
|
|
|
$current = $this->relatables->map(function (Relatable $relatable) { |
102
|
|
|
return $relatable->getRelatedValues(); |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
$items->each(function (array $values) { |
106
|
|
|
$this->relate($values['id'], $values['type']); |
107
|
|
|
}); |
108
|
|
|
|
109
|
|
|
if (!$detaching) { |
110
|
|
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$current |
114
|
|
|
->filter(function (array $values) use ($items) { |
115
|
|
|
return ! $items->contains($values); |
116
|
|
|
}) |
117
|
|
|
->each(function (array $values) { |
118
|
|
|
$this->unrelate($values['id'], $values['type']); |
119
|
|
|
}); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function getSyncRelatedValues($items) : Collection |
123
|
|
|
{ |
124
|
|
|
if ($items instanceof Collection) { |
125
|
|
|
return $items->map(function (Model $item) : array { |
126
|
|
|
return [ |
127
|
|
|
'type' => $item->getMorphClass(), |
128
|
|
|
'id' => $item->getKey(), |
129
|
|
|
]; |
130
|
|
|
}); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return collect($items); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $item |
138
|
|
|
* @param string|null $type |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
protected function getRelatableValues($item, string $type = '') : array |
143
|
|
|
{ |
144
|
|
|
if (! $item instanceof Model && empty($type)) { |
145
|
|
|
throw new \InvalidArgumentException( |
146
|
|
|
'If an id is specified as an item, the type isn\'t allowed to be empty.' |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return [ |
151
|
|
|
'source_id' => $this->getKey(), |
|
|
|
|
152
|
|
|
'source_type' => $this->getMorphClass(), |
|
|
|
|
153
|
|
|
'related_id' => $item instanceof Model ? $item->getKey() : $item, |
154
|
|
|
'related_type' => $item instanceof Model ? $item->getMorphClass() : $type, |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.