| 1: | <?php |
| 2: | |
| 3: | namespace Baril\Bonsai\Relations\Concerns; |
| 4: | |
| 5: | use Illuminate\Database\Eloquent\Builder; |
| 6: | use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
| 7: | use Illuminate\Database\Eloquent\Model; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | trait ExcludesSelf |
| 13: | { |
| 14: | |
| 15: | |
| 16: | |
| 17: | protected $excludeSelf = false; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | public function withoutSelf() |
| 23: | { |
| 24: | $this->excludeSelf = true; |
| 25: | |
| 26: | if (static::$constraints) { |
| 27: | |
| 28: | $this->getRelationQuery()->withGlobalScope( |
| 29: | 'excludeSelfFromResults', |
| 30: | function ($query) { |
| 31: | return $query->whereKeyNot($this->parent->getKey()); |
| 32: | } |
| 33: | ); |
| 34: | } |
| 35: | |
| 36: | return $this; |
| 37: | } |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | public function withSelf() |
| 43: | { |
| 44: | $this->excludeSelf = false; |
| 45: | |
| 46: | if (static::$constraints) { |
| 47: | $this->getRelationQuery()->withoutGlobalScope('excludeSelfFromResults'); |
| 48: | } |
| 49: | |
| 50: | return $this; |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | public function match(array $models, EloquentCollection $results, $relation) |
| 62: | { |
| 63: | return $this->excludeSelfFromMatchesIfExcluded( |
| 64: | parent::match($models, $results, $relation), |
| 65: | $relation |
| 66: | ); |
| 67: | } |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | protected function excludeSelfFromMatchesIfExcluded(array $models, $relation) |
| 75: | { |
| 76: | if (! $this->excludeSelf) { |
| 77: | return $models; |
| 78: | } |
| 79: | |
| 80: | foreach ($models as $model) { |
| 81: | $related = $model->getRelation($relation); |
| 82: | if ( |
| 83: | $related instanceof EloquentCollection |
| 84: | && $related->contains($model) |
| 85: | ) { |
| 86: | $model->setRelation($relation, $related->except($model->getKey())); |
| 87: | } |
| 88: | if ( |
| 89: | $related instanceof Model |
| 90: | && $related->getTable() == $model->getTable() |
| 91: | && $related->getKey() === $model->getKey() |
| 92: | ) { |
| 93: | $this->initRelation([$model], $relation); |
| 94: | } |
| 95: | } |
| 96: | |
| 97: | return $models; |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*']) |
| 111: | { |
| 112: | return $this->excludeSelfFromRelationExistenceQueryIfExcluded( |
| 113: | parent::getRelationExistenceQueryForSelfRelation($query, $parentQuery, $columns), |
| 114: | $parentQuery |
| 115: | ); |
| 116: | } |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | public function getRelationExistenceQueryForSelfJoin(Builder $query, Builder $parentQuery, $columns = ['*']) |
| 129: | { |
| 130: | return $this->excludeSelfFromRelationExistenceQueryIfExcluded( |
| 131: | parent::getRelationExistenceQueryForSelfJoin($query, $parentQuery, $columns), |
| 132: | $parentQuery |
| 133: | ); |
| 134: | } |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | protected function excludeSelfFromRelationExistenceQueryIfExcluded( |
| 142: | Builder $query, |
| 143: | Builder $parentQuery |
| 144: | ) { |
| 145: | return $query |
| 146: | ->when($this->excludeSelf, function ($query) use ($parentQuery) { |
| 147: | $query->withGlobalScope( |
| 148: | 'excludeSelfFromResults', |
| 149: | function ($query) use ($parentQuery) { |
| 150: | $query->whereColumn( |
| 151: | $parentQuery->qualifyColumn($this->parent->getKeyName()), |
| 152: | '!=', |
| 153: | $query->qualifyColumn($this->related->getKeyName()) |
| 154: | ); |
| 155: | } |
| 156: | ); |
| 157: | $query->macro('withSelf', function ($query) { |
| 158: | $query->withoutGlobalScope('excludeSelfFromResults'); |
| 159: | }); |
| 160: | }); |
| 161: | } |
| 162: | } |
| 163: | |