1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<?php namespace Illuminate\Database\Eloquent\Relations;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder;
class MorphToMany extends BelongsToMany {
/** * The type of the polymorphic relation. * * @var string */ protected $morphType;
/** * The class name of the morph type constraint. * * @var string */ protected $morphClass;
/** * Indicates if we are connecting the inverse of the relation. * * This primarily affects the morphClass constraint. * * @var bool */ protected $inverse;
/** * Create a new has many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $name * @param string $table * @param string $foreignKey * @param string $otherKey * @param string $relationName * @param bool $inverse * @return void */ public function __construct(Builder $query, Model $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false) { $this->inverse = $inverse; $this->morphType = $name.'_type'; $this->morphClass = $inverse ? get_class($query->getModel()) : get_class($parent);
parent::__construct($query, $parent, $table, $foreignKey, $otherKey, $relationName); }
/** * Set the where clause for the relation query. * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ protected function setWhere() { parent::setWhere();
$this->query->where($this->table.'.'.$this->morphType, $this->morphClass);
return $this; }
/** * Add the constraints for a relationship count query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parent * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationCountQuery(Builder $query, Builder $parent) { $query = parent::getRelationCountQuery($query, $parent);
return $query->where($this->table.'.'.$this->morphType, $this->morphClass); }
/** * Set the constraints for an eager load of the relation. * * @param array $models * @return void */ public function addEagerConstraints(array $models) { parent::addEagerConstraints($models);
$this->query->where($this->table.'.'.$this->morphType, $this->morphClass); }
/** * Create a new pivot attachment record. * * @param int $id * @param bool $timed * @return array */ protected function createAttachRecord($id, $timed) { $record = parent::createAttachRecord($id, $timed);
return array_add($record, $this->morphType, $this->morphClass); }
/** * Create a new query builder for the pivot table. * * @return \Illuminate\Database\Query\Builder */ protected function newPivotQuery() { $query = parent::newPivotQuery();
return $query->where($this->morphType, $this->morphClass); }
/** * Create a new pivot model instance. * * @param array $attributes * @param bool $exists * @return \Illuminate\Database\Eloquent\Relations\Pivot */ public function newPivot(array $attributes = array(), $exists = false) { $pivot = new MorphPivot($this->parent, $attributes, $this->table, $exists);
$pivot->setPivotKeys($this->foreignKey, $this->otherKey);
$pivot->setMorphType($this->morphType);
return $pivot; }
/** * Get the foreign key "type" name. * * @return string */ public function getMorphType() { return $this->morphType; }
/** * Get the class name of the parent model. * * @return string */ public function getMorphClass() { return $this->morphClass; }
}
|