Concerns
1 month ago
BelongsTo.php
2 years ago
BelongsToMany.php
1 month ago
HasMany.php
2 years ago
HasManyThrough.php
1 month ago
HasOne.php
1 month ago
HasOneOrMany.php
1 month ago
HasOneThrough.php
2 years ago
MorphMany.php
2 years ago
MorphOne.php
1 month ago
MorphOneOrMany.php
2 years ago
MorphPivot.php
1 month ago
MorphTo.php
6 months ago
MorphToMany.php
2 years ago
Pivot.php
2 years ago
Relation.php
1 month ago
MorphOneOrMany.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Eloquent\Relations; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Database\Eloquent\Builder; |
| 6 | use IAWPSCOPED\Illuminate\Database\Eloquent\Model; |
| 7 | /** @internal */ |
| 8 | abstract class MorphOneOrMany extends HasOneOrMany |
| 9 | { |
| 10 | /** |
| 11 | * The foreign key type for the relationship. |
| 12 | * |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $morphType; |
| 16 | /** |
| 17 | * The class name of the parent model. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | protected $morphClass; |
| 22 | /** |
| 23 | * Create a new morph one or many relationship instance. |
| 24 | * |
| 25 | * @param \Illuminate\Database\Eloquent\Builder $query |
| 26 | * @param \Illuminate\Database\Eloquent\Model $parent |
| 27 | * @param string $type |
| 28 | * @param string $id |
| 29 | * @param string $localKey |
| 30 | * @return void |
| 31 | */ |
| 32 | public function __construct(Builder $query, Model $parent, $type, $id, $localKey) |
| 33 | { |
| 34 | $this->morphType = $type; |
| 35 | $this->morphClass = $parent->getMorphClass(); |
| 36 | parent::__construct($query, $parent, $id, $localKey); |
| 37 | } |
| 38 | /** |
| 39 | * Set the base constraints on the relation query. |
| 40 | * |
| 41 | * @return void |
| 42 | */ |
| 43 | public function addConstraints() |
| 44 | { |
| 45 | if (static::$constraints) { |
| 46 | $this->getRelationQuery()->where($this->morphType, $this->morphClass); |
| 47 | parent::addConstraints(); |
| 48 | } |
| 49 | } |
| 50 | /** |
| 51 | * Set the constraints for an eager load of the relation. |
| 52 | * |
| 53 | * @param array $models |
| 54 | * @return void |
| 55 | */ |
| 56 | public function addEagerConstraints(array $models) |
| 57 | { |
| 58 | parent::addEagerConstraints($models); |
| 59 | $this->getRelationQuery()->where($this->morphType, $this->morphClass); |
| 60 | } |
| 61 | /** |
| 62 | * Set the foreign ID and type for creating a related model. |
| 63 | * |
| 64 | * @param \Illuminate\Database\Eloquent\Model $model |
| 65 | * @return void |
| 66 | */ |
| 67 | protected function setForeignAttributesForCreate(Model $model) |
| 68 | { |
| 69 | $model->{$this->getForeignKeyName()} = $this->getParentKey(); |
| 70 | $model->{$this->getMorphType()} = $this->morphClass; |
| 71 | } |
| 72 | /** |
| 73 | * Get the relationship query. |
| 74 | * |
| 75 | * @param \Illuminate\Database\Eloquent\Builder $query |
| 76 | * @param \Illuminate\Database\Eloquent\Builder $parentQuery |
| 77 | * @param array|mixed $columns |
| 78 | * @return \Illuminate\Database\Eloquent\Builder |
| 79 | */ |
| 80 | public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*']) |
| 81 | { |
| 82 | return parent::getRelationExistenceQuery($query, $parentQuery, $columns)->where($query->qualifyColumn($this->getMorphType()), $this->morphClass); |
| 83 | } |
| 84 | /** |
| 85 | * Get the foreign key "type" name. |
| 86 | * |
| 87 | * @return string |
| 88 | */ |
| 89 | public function getQualifiedMorphType() |
| 90 | { |
| 91 | return $this->morphType; |
| 92 | } |
| 93 | /** |
| 94 | * Get the plain morph type name without the table. |
| 95 | * |
| 96 | * @return string |
| 97 | */ |
| 98 | public function getMorphType() |
| 99 | { |
| 100 | return \IAWPSCOPED\last(\explode('.', $this->morphType)); |
| 101 | } |
| 102 | /** |
| 103 | * Get the class name of the parent model. |
| 104 | * |
| 105 | * @return string |
| 106 | */ |
| 107 | public function getMorphClass() |
| 108 | { |
| 109 | return $this->morphClass; |
| 110 | } |
| 111 | } |
| 112 |