1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\Relativity; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
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, |
24
|
|
|
$relatedPivotKey = null, $parentKey = null, |
25
|
|
|
$relatedKey = null, $inverse = false, $caller = null) |
26
|
|
|
{ |
27
|
1 |
|
$caller = $caller ?: $this->guessBelongsToManyRelation(); |
|
|
|
|
28
|
|
|
|
29
|
|
|
// First, we will need to determine the foreign key and "other key" for the |
30
|
|
|
// relationship. Once we have determined the keys we will make the query |
31
|
|
|
// instances, as well as the relationship instances we need for these. |
32
|
1 |
|
$instance = $this->newRelatedInstance($related); |
|
|
|
|
33
|
|
|
|
34
|
1 |
|
$foreignPivotKey = $foreignPivotKey ?: $name.'_id'; |
35
|
|
|
|
36
|
1 |
|
$relatedPivotKey = $relatedPivotKey ?: $instance->getForeignKey(); |
37
|
|
|
|
38
|
|
|
// Now we're ready to create a new query builder for this related model and |
39
|
|
|
// the relationship instances for this relation. This relations will set |
40
|
|
|
// appropriate query constraints then entirely manages the hydrations. |
41
|
1 |
|
$table = $table ?: Str::plural($name); |
42
|
|
|
|
43
|
1 |
|
return $this->newMorphToMany( |
|
|
|
|
44
|
1 |
|
$instance->newQuery(), $this, $name, $table, |
45
|
1 |
|
$foreignPivotKey, $relatedPivotKey, $parentKey ?: $this->getKeyName(), |
|
|
|
|
46
|
1 |
|
$relatedKey ?: $instance->getKeyName(), $caller, $inverse |
47
|
|
|
); |
48
|
|
|
} |
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
|
|
|
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, |
80
|
|
|
$relatedPivotKey = null, $parentKey = null, $relatedKey = null, $caller = null) |
81
|
|
|
{ |
82
|
|
|
$foreignPivotKey = $foreignPivotKey ?: $this->getForeignKey(); |
|
|
|
|
83
|
|
|
|
84
|
|
|
// For the inverse of the polymorphic many-to-many relations, we will change |
85
|
|
|
// the way we determine the foreign and other keys, as it is the opposite |
86
|
|
|
// of the morph-to-many method since we're figuring out these inverses. |
87
|
|
|
$relatedPivotKey = $relatedPivotKey ?: $name.'_id'; |
88
|
|
|
|
89
|
|
|
return $this->morphToMany( |
90
|
|
|
$related, $name, $table, $foreignPivotKey, |
91
|
|
|
$relatedPivotKey, $parentKey, $relatedKey, true, $caller |
92
|
|
|
); |
93
|
|
|
} |
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.