1 | <?php |
||
7 | trait BaseEloquentOverrides |
||
8 | { |
||
9 | /** |
||
10 | * Define a polymorphic many-to-many relationship. |
||
11 | * |
||
12 | * @param string $related |
||
13 | * @param string $name |
||
14 | * @param string $table |
||
15 | * @param string $foreignPivotKey |
||
16 | * @param string $relatedPivotKey |
||
17 | * @param string $parentKey |
||
18 | * @param string $relatedKey |
||
19 | * @param bool $inverse |
||
20 | * @param string $caller |
||
21 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
||
22 | */ |
||
23 | 1 | public function morphToMany($related, $name, $table = null, $foreignPivotKey = null, |
|
49 | |||
50 | 5 | public function getRelationValue($key) |
|
51 | { |
||
52 | // If the key already exists in the relationships array, it just means the |
||
53 | // relationship has already been loaded, so we'll just return it out of |
||
54 | // here because there is no need to query within the relations twice. |
||
55 | 5 | if ($this->relationLoaded($key)) { |
|
56 | 1 | return $this->relations[$key]; |
|
57 | } |
||
58 | |||
59 | // If the "attribute" exists as a method on the model, we will just assume |
||
60 | // it is a relationship and will load and return results from the query |
||
61 | // and hydrate the relationship's value on the "relationships" array. |
||
62 | 5 | if (method_exists($this, $key) or isset(static::$macros[$key])) { |
|
63 | 5 | return $this->getRelationshipFromMethod($key); |
|
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Define a polymorphic, inverse many-to-many relationship. |
||
69 | * |
||
70 | * @param string $related |
||
71 | * @param string $name |
||
72 | * @param string $table |
||
73 | * @param string $foreignPivotKey |
||
74 | * @param string $relatedPivotKey |
||
75 | * @param string $parentKey |
||
76 | * @param string $relatedKey |
||
77 | * @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
||
78 | */ |
||
79 | public function morphedByMany($related, $name, $table = null, $foreignPivotKey = null, |
||
94 | } |
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.