/**
* Add the constraints for a relationship count query on the same table.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parent
* @return \Illuminate\Database\Eloquent\Builder
*/
public function getRelationCountQueryForSelfRelation(Builder $query, Builder $parent)
{
$query->select(new Expression('count(*)'));
$query->from($query->getModel()->getTable() . ' as ' . ($hash = $this->getRelationCountHash()));
$query->getModel()->setTable($hash);
$key = $this->wrap($this->getQualifiedParentKeyName());
return $query->where($hash . '.' . $this->getPlainForeignKey(), '=', new Expression($key));
}