1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\Relativity; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
trait DynamicRelations |
8
|
|
|
{ |
9
|
|
|
use BaseEloquentOverrides; |
10
|
|
|
|
11
|
|
|
protected static $dynamicRelations = []; |
12
|
|
|
|
13
|
5 |
|
public static function defineRelation($name, $macro) |
14
|
|
|
{ |
15
|
5 |
|
static::$dynamicRelations[$name] = $macro; |
16
|
5 |
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Dynamically handle calls to the class. |
20
|
|
|
* |
21
|
|
|
* @param string $method |
22
|
|
|
* @param array $parameters |
23
|
|
|
* @return mixed |
24
|
|
|
* |
25
|
|
|
* @throws \BadMethodCallException |
26
|
|
|
*/ |
27
|
5 |
|
public function __call($method, $parameters) |
28
|
|
|
{ |
29
|
5 |
|
$dynamicRelation = static::$dynamicRelations[$method] ?? null; |
30
|
5 |
|
if (! $dynamicRelation) { |
31
|
5 |
|
return parent::__call($method, $parameters); |
32
|
|
|
} |
33
|
|
|
|
34
|
5 |
|
return call_user_func_array($dynamicRelation->bindTo($this, static::class), $parameters); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public static function morphed_by_many($relationName, $related, $name, $table = null, $foreignPivotKey = null, |
38
|
|
|
$relatedPivotKey = null, $parentKey = null, $relatedKey = null) |
39
|
|
|
{ |
40
|
1 |
|
return new AbstractRelation(['morphedByMany', static::class, $relationName, [$related, $name, $table, $foreignPivotKey, |
41
|
1 |
|
$relatedPivotKey, $parentKey, $relatedKey, $relationName]]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $related |
46
|
|
|
* @param $relationName |
47
|
|
|
* @param null $foreignKey |
48
|
|
|
* @param null $localKey |
49
|
|
|
* |
50
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
51
|
|
|
*/ |
52
|
1 |
|
public static function has_many($relationName = null, string $related, $foreignKey = null, $localKey = null) |
53
|
|
|
{ |
54
|
1 |
|
return new AbstractRelation(['hasMany', static::class, $relationName, [$related, $foreignKey, $localKey]]); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public static function has_one($relationName = null, $related, $foreignKey = null, $localKey = null) |
58
|
|
|
{ |
59
|
1 |
|
return new AbstractRelation(['hasOne', static::class, $relationName, [$related, $foreignKey, $localKey]]); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $related |
64
|
|
|
* @param $relationName |
65
|
|
|
* @param null $foreignKey |
66
|
|
|
* @param null $ownerKey |
67
|
|
|
* |
68
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
69
|
|
|
*/ |
70
|
1 |
|
public static function belongs_to($relationName = null, string $related, $foreignKey = null, $ownerKey = null) |
71
|
|
|
{ |
72
|
1 |
|
if (is_null($foreignKey)) { |
73
|
|
|
$foreignKey = Str::snake($relationName).'_id'; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return new AbstractRelation(['belongsTo', static::class, $relationName, [$related, $foreignKey, $ownerKey, $relationName]]); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public static function belongs_to_many($relationName, $related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, |
80
|
|
|
$parentKey = null, $relatedKey = null) |
81
|
|
|
{ |
82
|
1 |
|
$params = [$related, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey, $relationName]; |
83
|
|
|
|
84
|
1 |
|
return new AbstractRelation(['belongsToMany', static::class, $relationName, $params]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Define a polymorphic many-to-many relationship. |
89
|
|
|
* |
90
|
|
|
* @param string $relationName |
91
|
|
|
* @param string $related |
92
|
|
|
* @param string $name |
93
|
|
|
* @param string $table |
94
|
|
|
* @param string $foreignPivotKey |
95
|
|
|
* @param string $relatedPivotKey |
96
|
|
|
* @param string $parentKey |
97
|
|
|
* @param string $relatedKey |
98
|
|
|
* @param bool $inverse |
99
|
|
|
* |
100
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany |
101
|
|
|
*/ |
102
|
1 |
|
public static function morph_to_many($relationName, $related, $name, $table = null, $foreignPivotKey = null, |
103
|
|
|
$relatedPivotKey = null, $parentKey = null, |
104
|
|
|
$relatedKey = null, $inverse = false) |
105
|
|
|
{ |
106
|
1 |
|
$params = [$related, $name, $table, $foreignPivotKey, |
107
|
1 |
|
$relatedPivotKey, $parentKey, $relatedKey, $inverse, $relationName]; |
108
|
|
|
|
109
|
1 |
|
return new AbstractRelation(['morphToMany', static::class, $relationName, $params]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Define a polymorphic one-to-many relationship. |
114
|
|
|
* |
115
|
|
|
* @param string $relationName |
116
|
|
|
* @param string $related |
117
|
|
|
* @param string $name |
118
|
|
|
* @param string $type |
119
|
|
|
* @param string $id |
120
|
|
|
* @param string $localKey |
121
|
|
|
* |
122
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
123
|
|
|
*/ |
124
|
1 |
|
public static function morph_many($relationName, $related, $name, $type = null, $id = null, $localKey = null) |
125
|
|
|
{ |
126
|
1 |
|
$params = [$related, $name, $type, $id, $localKey]; |
127
|
|
|
|
128
|
1 |
|
return new AbstractRelation(['morphMany', static::class, $relationName, $params]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public static function morph_one($relationName, $related, $name, $type = null, $id = null, $localKey = null) |
132
|
|
|
{ |
133
|
|
|
$params = [$related, $name, $type, $id, $localKey]; |
134
|
|
|
return new AbstractRelation(['morphOne', static::class, $relationName, $params]); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public static function morph_to($relationName, $type = null, $id = null, $ownerKey = null) |
138
|
|
|
{ |
139
|
|
|
$params = [$relationName, $type, $id, $ownerKey]; |
140
|
|
|
|
141
|
|
|
return new AbstractRelation(['morphTo', static::class, $relationName, $params]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Define a has-many-through relationship. |
146
|
|
|
* |
147
|
|
|
* @param string $relationName |
148
|
|
|
* @param string $related |
149
|
|
|
* @param string $through |
150
|
|
|
* @param string|null $firstKey |
151
|
|
|
* @param string|null $secondKey |
152
|
|
|
* @param string|null $localKey |
153
|
|
|
* @param string|null $secondLocalKey |
154
|
|
|
* |
155
|
|
|
* @return \Imanghafoori\Relativity\AbstractRelation |
156
|
|
|
*/ |
157
|
|
|
public static function has_many_through($relationName, $related, $through, $firstKey = null, $secondKey = null, $localKey = null, $secondLocalKey = null) |
158
|
|
|
{ |
159
|
|
|
$params = [$related, $through, $firstKey, $secondKey, $localKey, $secondLocalKey]; |
160
|
|
|
|
161
|
|
|
return new AbstractRelation(['hasManyThrough', static::class, $relationName, $params]); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public static function forceEagerLoading(...$relation) |
165
|
|
|
{ |
166
|
|
|
static::registerModelEvent('booting', function ($model) use ($relation) { |
167
|
|
|
$model->with = $model->with + $relation; |
168
|
|
|
}); |
169
|
|
|
} |
170
|
|
|
} |