/** * Return all children for the specified node. * * @param Baum\Node $node * @return Illuminate\Database\Eloquent\Collection */ public function children($node) { $query = $this->node->newQuery(); $query->where($this->node->getQualifiedParentColumnName(), '=', $node->getKey()); // We must also add the scoped column values to the query to compute valid // left and right indexes. foreach ($this->scopedAttributes($node) as $fld => $value) { $query->where($this->qualify($fld), '=', $value); } $query->orderBy($this->node->getQualifiedLeftColumnName()); $query->orderBy($this->node->getQualifiedRightColumnName()); $query->orderBy($this->node->getQualifiedKeyName()); return $query->get(); }
/** * Checks if duplicate values for the column specified exist. Takes * the Nested Set scope columns into account (if appropiate). * * @param string $column * @return boolean */ protected function duplicatesExistForColumn($column) { $connection = $this->node->getConnection(); $grammar = $connection->getQueryGrammar(); $columns = array_merge($this->node->getQualifiedScopedColumns(), array($column)); $columnsForSelect = implode(', ', array_map(function ($col) use($grammar) { return $grammar->wrap($col); }, $columns)); $wrappedColumn = $grammar->wrap($column); $query = $this->node->newQuery()->select($connection->raw("{$columnsForSelect}, COUNT({$wrappedColumn})"))->havingRaw("COUNT({$wrappedColumn}) > 1"); foreach ($columns as $col) { $query->groupBy($col); } $result = $query->first(); return !is_null($result); }
/** * Applies a lock to the rows between the supplied index boundaries. * * @param int $lft * @param int $rgt * @return void */ protected function applyLockBetween($lft, $rgt) { $this->node->newQuery()->where($this->node->getLeftColumnName(), '>=', $lft)->where($this->node->getRightColumnName(), '<=', $rgt)->select($this->node->getKeyName())->lockForUpdate()->get(); }