From dd9434d697ec0ce73831d1566ad23a444739d2b0 Mon Sep 17 00:00:00 2001 From: Killian Thomas Date: Tue, 25 Sep 2018 08:02:37 +0200 Subject: [PATCH] Fix "ambiguous column" issue when using whereAncestorOr or whereDescendantOf with additional join(s) If joins are made between the table using the NodeTrait and the table itself, using conditions whereDescendantOf or whereAncestorOf would throw an Integrity Constraint Violation error on columns _lft and/or _rgt. --- src/QueryBuilder.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index b406c48..61aba6a 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -110,8 +110,9 @@ public function whereAncestorOf($id, $andSelf = false, $boolean = 'and') $this->query->whereNested(function ($inner) use ($value, $andSelf, $id, $keyName) { list($lft, $rgt) = $this->wrappedColumns(); + $wrappedTable = $this->query->getGrammar()->wrapTable($this->model->getTable()); - $inner->whereRaw("{$value} between {$lft} and {$rgt}"); + $inner->whereRaw("{$value} between {$wrappedTable}.{$lft} and {$wrappedTable}.{$rgt}"); if ( ! $andSelf) { $inner->where($keyName, '<>', $id); @@ -182,7 +183,7 @@ public function ancestorsAndSelf($id, array $columns = [ '*' ]) */ public function whereNodeBetween($values, $boolean = 'and', $not = false) { - $this->query->whereBetween($this->model->getLftName(), $values, $boolean, $not); + $this->query->whereBetween($this->model->getTable() . '.' . $this->model->getLftName(), $values, $boolean, $not); return $this; }