/** * Create a new has many relationship instance. * * @param \Nova\Database\ORM\Builder $query * @param \Nova\Database\ORM\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 ? $query->getModel()->getMorphClass() : $parent->getMorphClass(); parent::__construct($query, $parent, $table, $foreignKey, $otherKey, $relationName); }
/** * Add the constraints for a relationship count query on the same table. * * @param \Nova\Database\ORM\Builder $query * @param \Nova\Database\ORM\Builder $parent * @return \Nova\Database\ORM\Builder */ public function getRelationCountQueryForSelfJoin(Builder $query, Builder $parent) { $query->select(new Expression('count(*)')); $tablePrefix = $this->query->getQuery()->getConnection()->getTablePrefix(); $query->from($this->table . ' as ' . $tablePrefix . ($hash = $this->getRelationCountHash())); $key = $this->wrap($this->getQualifiedParentKeyName()); return $query->where($hash . '.' . $this->foreignKey, '=', new Expression($key)); }
/** * Set the keys for a save update query. * * @param \Nova\Database\ORM\Builder $query * @return \Nova\Database\ORM\Builder */ protected function setKeysForSaveQuery(Builder $query) { $query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery()); return $query; }
/** * Add the constraints for a relationship count query. * * @param \Nova\Database\ORM\Builder $query * @param \Nova\Database\ORM\Builder $parent * @return \Nova\Database\ORM\Builder */ public function getRelationCountQuery(Builder $query, Builder $parent) { $query->select(new Expression('count(*)')); $otherKey = $this->wrap($query->getModel()->getTable() . '.' . $this->otherKey); return $query->where($this->getQualifiedForeignKey(), '=', new Expression($otherKey)); }
/** * Get the base query builder driving the ORM builder. * * @return \Nova\Database\Query\Builder */ public function getBaseQuery() { return $this->query->getQuery(); }
/** * Set the keys for a save update query. * * @param \Nova\Database\ORM\Builder $query * @return \Nova\Database\ORM\Builder */ protected function setKeysForSaveQuery(Builder $query) { $query->where($this->morphType, $this->morphClass); return parent::setKeysForSaveQuery($query); }
/** * Set the keys for a save update query. * * @param \Nova\Database\ORM\Builder * @return \Nova\Database\ORM\Builder */ protected function setKeysForSaveQuery(Builder $query) { $query->where($this->foreignKey, $this->getAttribute($this->foreignKey)); return $query->where($this->otherKey, $this->getAttribute($this->otherKey)); }
/** * Return trashed models with query if told so * * @param \Nova\Database\ORM\Builder $query * @return \Nova\Database\ORM\Builder */ protected function useWithTrashed(Builder $query) { if ($this->withTrashed && $query->getMacro('withTrashed') !== null) { return $query->withTrashed(); } return $query; }