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() : Collection |
41
|
|
|
{ |
42
|
|
|
$this->load('relatables'); |
|
|
|
|
43
|
|
|
|
44
|
|
|
return $this->relatableCache = $this->relatables |
45
|
|
|
->groupBy(function (Relatable $relatable) { |
46
|
|
|
return $this->getActualClassNameForMorph($relatable->related_type); |
|
|
|
|
47
|
|
|
}) |
48
|
|
|
->flatMap(function (Collection $typeGroup, string $type) { |
49
|
|
|
return $type::whereIn('id', $typeGroup->pluck('related_id'))->get(); |
50
|
|
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function hasRelated() : bool |
54
|
|
|
{ |
55
|
|
|
return ! $this->related->isEmpty(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The `$item` parameter must be an Eloquent model or an ID. If you provide an ID, the model's |
60
|
|
|
* morph type must be specified as a second parameter. |
61
|
|
|
* |
62
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $item |
63
|
|
|
* @param string|null $type |
64
|
|
|
* |
65
|
|
|
* @return \Spatie\Relatable\Relatable |
66
|
|
|
*/ |
67
|
|
|
public function relate($item, string $type = '') : Relatable |
68
|
|
|
{ |
69
|
|
|
return Relatable::firstOrCreate( |
70
|
|
|
$this->getRelatableValues($item, $type) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The `$item` parameter must be an Eloquent model or an ID. If you provide an ID, the model's |
76
|
|
|
* morph type must be specified as a second parameter. |
77
|
|
|
* |
78
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $item |
79
|
|
|
* @param string|null $type |
80
|
|
|
* |
81
|
|
|
* @return int |
82
|
|
|
*/ |
83
|
|
|
public function unrelate($item, string $type = '') : int |
84
|
|
|
{ |
85
|
|
|
return Relatable::where($this->getRelatableValues($item, $type))->delete(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* The `$items` parameter can either contain an Eloquent collection of models, or an array |
90
|
|
|
* with the shape of [['id' => int, 'type' => string], ...]. |
91
|
|
|
* |
92
|
|
|
* @param \Illuminate\Database\Eloquent\Collection|array $items |
93
|
|
|
* @param bool $detaching |
94
|
|
|
*/ |
95
|
|
|
public function syncRelated($items, $detaching = true) |
96
|
|
|
{ |
97
|
|
|
$items = $this->getSyncRelatedValues($items); |
98
|
|
|
|
99
|
|
|
$current = $this->relatables->map(function (Relatable $relatable) { |
100
|
|
|
return $relatable->getRelatedValues(); |
101
|
|
|
}); |
102
|
|
|
|
103
|
|
|
$items->each(function (array $values) { |
104
|
|
|
$this->relate($values['id'], $values['type']); |
105
|
|
|
}); |
106
|
|
|
|
107
|
|
|
if (!$detaching) { |
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$current |
112
|
|
|
->filter(function (array $values) use ($items) { |
113
|
|
|
return ! $items->contains($values); |
114
|
|
|
}) |
115
|
|
|
->each(function (array $values) { |
116
|
|
|
$this->unrelate($values['id'], $values['type']); |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function getSyncRelatedValues($items) : Collection |
121
|
|
|
{ |
122
|
|
|
if ($items instanceof Collection) { |
123
|
|
|
return $items->map(function (Model $item) : array { |
124
|
|
|
return [ |
125
|
|
|
'type' => $item->getMorphClass(), |
126
|
|
|
'id' => $item->getKey(), |
127
|
|
|
]; |
128
|
|
|
}); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return collect($items); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param \Illuminate\Database\Eloquent\Model|int $item |
136
|
|
|
* @param string|null $type |
137
|
|
|
* |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
protected function getRelatableValues($item, string $type = '') : array |
141
|
|
|
{ |
142
|
|
|
if (! $item instanceof Model && empty($type)) { |
143
|
|
|
throw new \InvalidArgumentException( |
144
|
|
|
'If an id is specified as an item, the type isn\'t allowed to be empty.' |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return [ |
149
|
|
|
'source_id' => $this->getKey(), |
|
|
|
|
150
|
|
|
'source_type' => $this->getMorphClass(), |
|
|
|
|
151
|
|
|
'related_id' => $item instanceof Model ? $item->getKey() : $item, |
152
|
|
|
'related_type' => $item instanceof Model ? $item->getMorphClass() : $type, |
153
|
|
|
]; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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.