1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Eloquent\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder as IlluminateBuilder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model as IlluminateModel; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\BelongsTo; |
9
|
|
|
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\HasOne; |
10
|
|
|
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\MorphOne; |
11
|
|
|
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\MorphTo; |
12
|
|
|
use LaravelFreelancerNL\Aranguent\Eloquent\Relations\MorphMany; |
13
|
|
|
|
14
|
|
|
trait HasRelationships |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Define a one-to-one relationship. |
18
|
|
|
* |
19
|
|
|
* @param string $related |
20
|
|
|
* @param string $foreignKey |
21
|
|
|
* @param string $localKey |
22
|
|
|
* @return HasOne |
23
|
|
|
*/ |
24
|
|
|
public function hasOne($related, $foreignKey = null, $localKey = null) |
25
|
|
|
{ |
26
|
|
|
$instance = $this->newRelatedInstance($related); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$foreignKey = $foreignKey ?: $this->getForeignKey(); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$localKey = $localKey ?: $this->getKeyName(); |
|
|
|
|
31
|
|
|
|
32
|
|
|
return $this->newHasOne($instance->newQuery(), $this, $instance->qualifyColumn($foreignKey), $localKey); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Define an inverse one-to-one or many relationship. |
37
|
|
|
* |
38
|
|
|
* @param string $related |
39
|
|
|
* @param string $foreignKey |
40
|
|
|
* @param string $ownerKey |
41
|
|
|
* @param string $relation |
42
|
|
|
* @return BelongsTo |
43
|
|
|
*/ |
44
|
|
|
public function belongsTo($related, $foreignKey = null, $ownerKey = null, $relation = null) |
45
|
|
|
{ |
46
|
|
|
// If no relation name was given, we will use this debug backtrace to extract |
47
|
|
|
// the calling method's name and use that as the relationship name as most |
48
|
|
|
// of the time this will be what we desire to use for the relationships. |
49
|
|
|
if (is_null($relation)) { |
50
|
|
|
$relation = $this->guessBelongsToRelation(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$instance = $this->newRelatedInstance($related); |
54
|
|
|
|
55
|
|
|
// If no foreign key was supplied, we can use a backtrace to guess the proper |
56
|
|
|
// foreign key name by using the name of the relationship function, which |
57
|
|
|
// when combined with an "_id" should conventionally match the columns. |
58
|
|
|
if (is_null($foreignKey)) { |
59
|
|
|
$foreignKey = Str::snake($relation) . $instance->getKeyName(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Once we have the foreign key names, we'll just create a new Eloquent query |
63
|
|
|
// for the related models and returns the relationship instance which will |
64
|
|
|
// actually be responsible for retrieving and hydrating every relations. |
65
|
|
|
$ownerKey = $ownerKey ?: $instance->getKeyName(); |
66
|
|
|
|
67
|
|
|
return new BelongsTo($instance->newQuery(), $this, $foreignKey, $ownerKey, $relation); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Define a one-to-many relationship. |
72
|
|
|
* |
73
|
|
|
* @param string $related |
74
|
|
|
* @param string|null $foreignKey |
75
|
|
|
* @param string|null $localKey |
76
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
77
|
|
|
*/ |
78
|
|
|
public function hasMany($related, $foreignKey = null, $localKey = null) |
79
|
|
|
{ |
80
|
|
|
$instance = $this->newRelatedInstance($related); |
81
|
|
|
|
82
|
|
|
$foreignKey = $foreignKey ?: $this->getForeignKey(); |
83
|
|
|
|
84
|
|
|
$localKey = $localKey ?: $this->getKeyName(); |
85
|
|
|
|
86
|
|
|
return $this->newHasMany($instance->newQuery(), $this, $foreignKey, $localKey); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Define a polymorphic one-to-one relationship. |
91
|
|
|
* |
92
|
|
|
* @param string $related |
93
|
|
|
* @param string $name |
94
|
|
|
* @param string|null $type |
95
|
|
|
* @param string|null $id |
96
|
|
|
* @param string|null $localKey |
97
|
|
|
* @return MorphOne |
98
|
|
|
*/ |
99
|
|
|
public function morphOne($related, $name, $type = null, $id = null, $localKey = null) |
100
|
|
|
{ |
101
|
|
|
$instance = $this->newRelatedInstance($related); |
102
|
|
|
|
103
|
|
|
[$type, $id] = $this->getMorphs($name, $type, $id); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$localKey = $localKey ?: $this->getKeyName(); |
106
|
|
|
|
107
|
|
|
return $this->newMorphOne($instance->newQuery(), $this, $type, $id, $localKey); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Instantiate a new MorphOne relationship. |
112
|
|
|
* |
113
|
|
|
* @param IlluminateBuilder $query |
114
|
|
|
* @param IlluminateModel $parent |
115
|
|
|
* @param string $type |
116
|
|
|
* @param string $id |
117
|
|
|
* @param string $localKey |
118
|
|
|
* @return MorphOne |
119
|
|
|
*/ |
120
|
|
|
protected function newMorphOne(IlluminateBuilder $query, IlluminateModel $parent, $type, $id, $localKey) |
121
|
|
|
{ |
122
|
|
|
return new MorphOne($query, $parent, $type, $id, $localKey); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Instantiate a new MorphTo relationship. |
127
|
|
|
* |
128
|
|
|
* @param IlluminateBuilder $query |
129
|
|
|
* @param IlluminateModel $parent |
130
|
|
|
* @param string $foreignKey |
131
|
|
|
* @param string $ownerKey |
132
|
|
|
* @param string $type |
133
|
|
|
* @param string $relation |
134
|
|
|
* @return MorphTo |
135
|
|
|
*/ |
136
|
|
|
protected function newMorphTo( |
137
|
|
|
IlluminateBuilder $query, |
138
|
|
|
IlluminateModel $parent, |
139
|
|
|
$foreignKey, |
140
|
|
|
$ownerKey, |
141
|
|
|
$type, |
142
|
|
|
$relation |
143
|
|
|
) { |
144
|
|
|
return new MorphTo($query, $parent, $foreignKey, $ownerKey, $type, $relation); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Define a polymorphic one-to-many relationship. |
149
|
|
|
* |
150
|
|
|
* @param string $related |
151
|
|
|
* @param string $name |
152
|
|
|
* @param string|null $type |
153
|
|
|
* @param string|null $id |
154
|
|
|
* @param string|null $localKey |
155
|
|
|
* @return MorphMany |
156
|
|
|
*/ |
157
|
|
|
public function morphMany($related, $name, $type = null, $id = null, $localKey = null) |
158
|
|
|
{ |
159
|
|
|
$instance = $this->newRelatedInstance($related); |
160
|
|
|
|
161
|
|
|
// Here we will gather up the morph type and ID for the relationship so that we |
162
|
|
|
// can properly query the intermediate table of a relation. Finally, we will |
163
|
|
|
// get the table and create the relationship instances for the developers. |
164
|
|
|
[$type, $id] = $this->getMorphs($name, $type, $id); |
165
|
|
|
|
166
|
|
|
$localKey = $localKey ?: $this->getKeyName(); |
167
|
|
|
|
168
|
|
|
return $this->newMorphMany($instance->newQuery(), $this, $type, $id, $localKey); |
|
|
|
|
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Instantiate a new MorphMany relationship. |
173
|
|
|
* |
174
|
|
|
* @param IlluminateBuilder $query |
175
|
|
|
* @param IlluminateModel $parent |
176
|
|
|
* @param string $type |
177
|
|
|
* @param string $id |
178
|
|
|
* @param string $localKey |
179
|
|
|
* @return MorphMany |
180
|
|
|
*/ |
181
|
|
|
protected function newMorphMany(IlluminateBuilder $query, IlluminateModel $parent, $type, $id, $localKey) |
182
|
|
|
{ |
183
|
|
|
return new MorphMany($query, $parent, $type, $id, $localKey); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|