1 | <?php |
||
15 | trait HasRelatedContent |
||
16 | { |
||
17 | /** @var \Illuminate\Support\Collection|null */ |
||
18 | protected $relatableCache; |
||
19 | |||
20 | public function relatables() : MorphMany |
||
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 |
||
39 | |||
40 | public function loadRelated($reloadRelatables = true) : Collection |
||
54 | |||
55 | public function hasRelated() : bool |
||
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 |
||
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 |
||
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) |
||
121 | |||
122 | protected function getSyncRelatedValues($items) : Collection |
||
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 |
||
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.