/**
  * Create a new relation instance.
  *
  * @param  \Database\ORM\Builder  $query
  * @param  \Database\ORM\Model  $parent
  * @return void
  */
 public function __construct(Builder $query, Model $parent)
 {
     $this->query = $query;
     $this->parent = $parent;
     $this->related = $query->getModel();
     $this->addConstraints();
 }
 /**
  * Create a new has many relationship instance.
  *
  * @param  \Database\ORM\Builder  $query
  * @param  \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.
  *
  * @param  \Database\ORM\Builder  $query
  * @param  \Database\ORM\Builder  $parent
  * @return \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));
 }