/** * @param \Notadd\Foundation\Database\Eloquent\Builder $query * @param \Notadd\Foundation\Database\Eloquent\Builder $parent * @return \Notadd\Foundation\Database\Eloquent\Builder */ public function getRelationCountQueryForSelfRelation(Builder $query, Builder $parent) { $query->select(new Expression('count(*)')); $query->from($query->getModel()->getTable() . ' as ' . ($hash = $this->getRelationCountHash())); $key = $this->wrap($this->getQualifiedParentKeyName()); return $query->where($hash . '.' . $this->getPlainForeignKey(), '=', new Expression($key)); }
/** * MorphToMany constructor. * @param \Notadd\Foundation\Database\Eloquent\Builder $query * @param \Notadd\Foundation\Database\Eloquent\Model $parent * @param string $name * @param string $table * @param string $foreignKey * @param string $otherKey * @param string $relationName * @param bool $inverse */ 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 ? $query->getModel()->getMorphClass() : $parent->getMorphClass(); parent::__construct($query, $parent, $table, $foreignKey, $otherKey, $relationName); }
/** * @return \Notadd\Foundation\Database\Query\Builder */ public function getBaseQuery() { return $this->query->getQuery(); }
/** * @param \Notadd\Foundation\Database\Eloquent\Builder $query * @return \Notadd\Foundation\Database\Eloquent\Builder */ protected function setKeysForSaveQuery(Builder $query) { $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery()); return $query; }
/** * @param \Notadd\Foundation\Database\Eloquent\Builder $query * @return \Notadd\Foundation\Database\Eloquent\Builder */ protected function useWithTrashed(Builder $query) { if ($this->withTrashed && $query->getMacro('withTrashed') !== null) { return $query->withTrashed(); } return $query; }
/** * @param \Notadd\Foundation\Database\Eloquent\Builder $query * @return \Notadd\Foundation\Database\Eloquent\Builder */ protected function setKeysForSaveQuery(Builder $query) { $query->where($this->foreignKey, $this->getAttribute($this->foreignKey)); return $query->where($this->otherKey, $this->getAttribute($this->otherKey)); }
/** * @param \Notadd\Foundation\Database\Eloquent\Builder $query * @return \Notadd\Foundation\Database\Eloquent\Builder */ protected function setKeysForSaveQuery(Builder $query) { $query->where($this->morphType, $this->morphClass); return parent::setKeysForSaveQuery($query); }