From 5584cd447e9331cccfece9a95b49f0d36ad0daaf Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Thu, 13 Jan 2022 20:09:03 -0500 Subject: [PATCH 1/5] Fix toBase() call --- src/Illuminate/Database/Eloquent/Relations/Relation.php | 2 +- tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index a45f5f3c9b06..161782ad64a4 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -314,7 +314,7 @@ public function getBaseQuery() */ public function toBase() { - return $this->query->getQuery(); + return $this->query->toBase(); } /** diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index 786c9c35242b..3b6619990048 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -473,6 +473,7 @@ public function testHasManyRelationshipCanBeSoftDeleted() $this->assertCount(1, $abigail->posts); $this->assertSame('First Title', $abigail->posts->first()->title); + $this->assertCount(1, $abigail->posts()->get()); $this->assertCount(2, $abigail->posts()->withTrashed()->get()); // restore @@ -504,6 +505,7 @@ public function testSecondLevelRelationshipCanBeSoftDeleted() $abigail = $abigail->fresh(); $this->assertCount(0, $abigail->posts()->first()->comments); + $this->assertCount(0, $abigail->posts()->first()->comments()->get()); $this->assertCount(1, $abigail->posts()->first()->comments()->withTrashed()->get()); } From 340dd28d2fdce508465148199ec475d25a7703ee Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Thu, 13 Jan 2022 20:12:15 -0500 Subject: [PATCH 2/5] Restore query builder interface --- .../Contracts/Database/Query/Builder.php | 1110 +++++++++++++++ src/Illuminate/Database/Eloquent/Builder.php | 12 +- .../Concerns/DecoratesQueryBuilder.php | 1225 +++++++++++++++++ .../Database/Eloquent/Relations/MorphTo.php | 42 +- .../Database/Eloquent/Relations/Relation.php | 6 +- src/Illuminate/Database/Query/Builder.php | 759 ++-------- 6 files changed, 2504 insertions(+), 650 deletions(-) create mode 100644 src/Illuminate/Contracts/Database/Query/Builder.php create mode 100644 src/Illuminate/Database/Eloquent/Concerns/DecoratesQueryBuilder.php diff --git a/src/Illuminate/Contracts/Database/Query/Builder.php b/src/Illuminate/Contracts/Database/Query/Builder.php new file mode 100644 index 000000000000..99de21e3a049 --- /dev/null +++ b/src/Illuminate/Contracts/Database/Query/Builder.php @@ -0,0 +1,1110 @@ +callNamedScope($method, $parameters); } - if (in_array($method, $this->passthru)) { - return $this->toBase()->{$method}(...$parameters); - } - $this->forwardCallTo($this->query, $method, $parameters); return $this; diff --git a/src/Illuminate/Database/Eloquent/Concerns/DecoratesQueryBuilder.php b/src/Illuminate/Database/Eloquent/Concerns/DecoratesQueryBuilder.php new file mode 100644 index 000000000000..51f4b61a87c5 --- /dev/null +++ b/src/Illuminate/Database/Eloquent/Concerns/DecoratesQueryBuilder.php @@ -0,0 +1,1225 @@ +forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function selectSub($query, $as) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function selectRaw($expression, array $bindings = []) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function fromSub($query, $as) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function fromRaw($expression, $bindings = []) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function addSelect($column) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function distinct() + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function from($table, $as = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function joinWhere($table, $first, $operator, $second, $type = 'inner') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function leftJoin($table, $first, $operator = null, $second = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function leftJoinWhere($table, $first, $operator, $second) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function leftJoinSub($query, $as, $first, $operator = null, $second = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function rightJoin($table, $first, $operator = null, $second = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function rightJoinWhere($table, $first, $operator, $second) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function rightJoinSub($query, $as, $first, $operator = null, $second = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function crossJoin($table, $first = null, $operator = null, $second = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function crossJoinSub($query, $as) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function mergeWheres($wheres, $bindings) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function where($column, $operator = null, $value = null, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function prepareValueAndOperator($value, $operator, $useDefault = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhere($column, $operator = null, $value = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereColumn($first, $operator = null, $second = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereRaw($sql, $bindings = [], $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereRaw($sql, $bindings = []) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereIn($column, $values, $boolean = 'and', $not = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereIn($column, $values) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereNotIn($column, $values, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereNotIn($column, $values) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereIntegerInRaw($column, $values, $boolean = 'and', $not = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereIntegerInRaw($column, $values) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereIntegerNotInRaw($column, $values, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereIntegerNotInRaw($column, $values) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereNull($columns, $boolean = 'and', $not = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereNull($column) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereNotNull($columns, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereBetween($column, iterable $values, $boolean = 'and', $not = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereBetweenColumns($column, array $values, $boolean = 'and', $not = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereBetween($column, iterable $values) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereBetweenColumns($column, array $values) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereNotBetween($column, iterable $values, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereNotBetweenColumns($column, array $values, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereNotBetween($column, iterable $values) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereNotBetweenColumns($column, array $values) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereNotNull($column) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereDate($column, $operator, $value = null, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereDate($column, $operator, $value = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereTime($column, $operator, $value = null, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereTime($column, $operator, $value = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereDay($column, $operator, $value = null, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereDay($column, $operator, $value = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereMonth($column, $operator, $value = null, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereMonth($column, $operator, $value = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereYear($column, $operator, $value = null, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereYear($column, $operator, $value = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereNested(Closure $callback, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function addNestedWhereQuery($query, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereExists(Closure $callback, $boolean = 'and', $not = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereExists(Closure $callback, $not = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereNotExists(Closure $callback, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereNotExists(Closure $callback) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereRowValues($columns, $operator, $values, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereRowValues($columns, $operator, $values) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereJsonContains($column, $value, $boolean = 'and', $not = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereJsonContains($column, $value) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereJsonDoesntContain($column, $value, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereJsonDoesntContain($column, $value) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereJsonLength($column, $operator, $value = null, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereJsonLength($column, $operator, $value = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function whereFullText($columns, $value, array $options = [], $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orWhereFullText($columns, $value, array $options = []) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function groupBy(...$groups) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function groupByRaw($sql, array $bindings = []) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function having($column, $operator = null, $value = null, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orHaving($column, $operator = null, $value = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function havingNull($columns, $boolean = 'and', $not = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * {@inheritdoc} + */ + public function orHavingNull($column) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function havingNotNull($columns, $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function orHavingNotNull($column) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function havingBetween($column, array $values, $boolean = 'and', $not = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function havingRaw($sql, array $bindings = [], $boolean = 'and') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function orHavingRaw($sql, array $bindings = []) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function orderBy($column, $direction = 'asc') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function orderByDesc($column) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function latest($column = 'created_at') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function oldest($column = 'created_at') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function inRandomOrder($seed = '') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function orderByRaw($sql, $bindings = []) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function skip($value) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function offset($value) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function take($value) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function limit($value) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function forPage($page, $perPage = 15) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function forPageBeforeId($perPage = 15, $lastId = 0, $column = 'id') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function reorder($column = null, $direction = 'asc') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function union($query, $all = false) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function unionAll($query) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function lock($value = true) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function lockForUpdate() + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function sharedLock() + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function toSql() + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function find($id, $columns = ['*']) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function value($column) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function get($columns = ['*']) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function cursor() + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function pluck($column, $key = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function implode($column, $glue = '') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function exists() + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function doesntExist() + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function existsOr(Closure $callback) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function doesntExistOr(Closure $callback) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function count($columns = '*') + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function min($column) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function max($column) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function sum($column) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function avg($column) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function average($column) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function aggregate($function, $columns = ['*']) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function numericAggregate($function, $columns = ['*']) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function insert(array $values) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function insertOrIgnore(array $values) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function insertGetId(array $values, $sequence = null) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function insertUsing(array $columns, $query) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function update(array $values) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function updateOrInsert(array $attributes, array $values = []) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function upsert(array $values, $uniqueBy, $update = null) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function increment($column, $amount = 1, array $extra = []) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function decrement($column, $amount = 1, array $extra = []) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function delete() + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function truncate() + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function newQuery() + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function raw($value) + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function getBindings() + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function getRawBindings() + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function setBindings(array $bindings, $type = 'where') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function addBinding($value, $type = 'where') + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function mergeBindings(Builder $query) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function cleanBindings(array $bindings) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function getConnection() + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function getProcessor() + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function getGrammar() + { + return $this->toBase()->{__FUNCTION__}(...func_get_args()); + } + + /** + * @inheritdoc + */ + public function useWritePdo() + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function clone() + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function cloneWithout(array $properties) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * @inheritdoc + */ + public function cloneWithoutBindings(array $except) + { + return $this->forwardDecoratedCallTo($this->query, __FUNCTION__, func_get_args()); + } + + /** + * Handle dynamic method calls to the query builder. + * + * @param string $method + * @param array $parameters + * @return mixed + */ + public function __call($method, $parameters) + { + return $this->forwardDecoratedCallTo($this->query, $method, $parameters); + } +} diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index 262741f30cfb..811853fb0c61 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -79,6 +79,46 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $ownerKe parent::__construct($query, $parent, $foreignKey, $ownerKey, $relation); } + /** + * {@inheritdoc} + */ + public function select($columns = ['*']) + { + $this->macroBuffer[] = ['method' => 'select', 'parameters' => [$columns]]; + + return parent::select($columns); + } + + /** + * {@inheritdoc} + */ + public function selectRaw($expression, array $bindings = []) + { + $this->macroBuffer[] = ['method' => 'selectRaw', 'parameters' => [$expression, $bindings]]; + + return parent::selectRaw($expression, $bindings); + } + + /** + * {@inheritdoc} + */ + public function selectSub($query, $as) + { + $this->macroBuffer[] = ['method' => 'selectSub', 'parameters' => [$query, $as]]; + + return parent::selectSub($query, $as); + } + + /** + * {@inheritdoc} + */ + public function addSelect($column) + { + $this->macroBuffer[] = ['method' => 'addSelect', 'parameters' => [$column]]; + + return parent::addSelect($column); + } + /** * Set the constraints for an eager load of the relation. * @@ -377,7 +417,7 @@ public function __call($method, $parameters) try { $result = parent::__call($method, $parameters); - if (in_array($method, ['select', 'selectRaw', 'selectSub', 'addSelect', 'withoutGlobalScopes'])) { + if ($method === 'withoutGlobalScopes') { $this->macroBuffer[] = compact('method', 'parameters'); } diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index 161782ad64a4..ba8c036dd3e8 100755 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -3,8 +3,10 @@ namespace Illuminate\Database\Eloquent\Relations; use Closure; +use Illuminate\Contracts\Database\Query\Builder as BuilderContract; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Concerns\DecoratesQueryBuilder; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\MultipleRecordsFoundException; @@ -13,9 +15,9 @@ use Illuminate\Support\Traits\ForwardsCalls; use Illuminate\Support\Traits\Macroable; -abstract class Relation +abstract class Relation implements BuilderContract { - use ForwardsCalls, Macroable { + use DecoratesQueryBuilder, ForwardsCalls, Macroable { Macroable::__call as macroCall; } diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index e675e6063d05..a602ad85873b 100755 --- a/src/Illuminate/Database/Query/Builder.php +++ b/src/Illuminate/Database/Query/Builder.php @@ -6,6 +6,7 @@ use Carbon\CarbonPeriod; use Closure; use DateTimeInterface; +use Illuminate\Contracts\Database\Query\Builder as BuilderContract; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Database\Concerns\BuildsQueries; use Illuminate\Database\Concerns\ExplainsQueries; @@ -25,7 +26,7 @@ use LogicException; use RuntimeException; -class Builder +class Builder implements BuilderContract { use BuildsQueries, ExplainsQueries, ForwardsCalls, Macroable { __call as macroCall; @@ -229,16 +230,12 @@ public function __construct(ConnectionInterface $connection, } /** - * Set the columns to be selected. - * - * @param array|mixed $columns - * @return $this + * {@inheritdoc} */ public function select($columns = ['*']) { $this->columns = []; $this->bindings['select'] = []; - $columns = is_array($columns) ? $columns : func_get_args(); foreach ($columns as $as => $column) { @@ -253,13 +250,7 @@ public function select($columns = ['*']) } /** - * Add a subselect expression to the query. - * - * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query - * @param string $as - * @return $this - * - * @throws \InvalidArgumentException + * {@inheritdoc} */ public function selectSub($query, $as) { @@ -271,11 +262,7 @@ public function selectSub($query, $as) } /** - * Add a new "raw" select expression to the query. - * - * @param string $expression - * @param array $bindings - * @return $this + * {@inheritdoc} */ public function selectRaw($expression, array $bindings = []) { @@ -289,13 +276,7 @@ public function selectRaw($expression, array $bindings = []) } /** - * Makes "from" fetch from a subquery. - * - * @param \Closure|\Illuminate\Database\Query\Builder|string $query - * @param string $as - * @return $this - * - * @throws \InvalidArgumentException + * {@inheritdoc} */ public function fromSub($query, $as) { @@ -305,11 +286,7 @@ public function fromSub($query, $as) } /** - * Add a raw from clause to the query. - * - * @param string $expression - * @param mixed $bindings - * @return $this + * {@inheritdoc} */ public function fromRaw($expression, $bindings = []) { @@ -384,10 +361,7 @@ protected function prependDatabaseNameIfCrossDatabaseQuery($query) } /** - * Add a new select column to the query. - * - * @param array|mixed $column - * @return $this + * {@inheritdoc} */ public function addSelect($column) { @@ -409,9 +383,7 @@ public function addSelect($column) } /** - * Force the query to only return distinct results. - * - * @return $this + * {@inheritdoc} */ public function distinct() { @@ -427,11 +399,7 @@ public function distinct() } /** - * Set the table which the query is targeting. - * - * @param \Closure|\Illuminate\Database\Query\Builder|string $table - * @param string|null $as - * @return $this + * {@inheritdoc} */ public function from($table, $as = null) { @@ -445,15 +413,7 @@ public function from($table, $as = null) } /** - * Add a join clause to the query. - * - * @param string $table - * @param \Closure|string $first - * @param string|null $operator - * @param string|null $second - * @param string $type - * @param bool $where - * @return $this + * {@inheritdoc} */ public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false) { @@ -485,14 +445,7 @@ public function join($table, $first, $operator = null, $second = null, $type = ' } /** - * Add a "join where" clause to the query. - * - * @param string $table - * @param \Closure|string $first - * @param string $operator - * @param string $second - * @param string $type - * @return $this + * {@inheritdoc} */ public function joinWhere($table, $first, $operator, $second, $type = 'inner') { @@ -500,18 +453,7 @@ public function joinWhere($table, $first, $operator, $second, $type = 'inner') } /** - * Add a subquery join clause to the query. - * - * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query - * @param string $as - * @param \Closure|string $first - * @param string|null $operator - * @param string|null $second - * @param string $type - * @param bool $where - * @return $this - * - * @throws \InvalidArgumentException + * {@inheritdoc} */ public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false) { @@ -525,13 +467,7 @@ public function joinSub($query, $as, $first, $operator = null, $second = null, $ } /** - * Add a left join to the query. - * - * @param string $table - * @param \Closure|string $first - * @param string|null $operator - * @param string|null $second - * @return $this + * {@inheritdoc} */ public function leftJoin($table, $first, $operator = null, $second = null) { @@ -539,13 +475,7 @@ public function leftJoin($table, $first, $operator = null, $second = null) } /** - * Add a "join where" clause to the query. - * - * @param string $table - * @param \Closure|string $first - * @param string $operator - * @param string $second - * @return $this + * {@inheritdoc} */ public function leftJoinWhere($table, $first, $operator, $second) { @@ -553,14 +483,7 @@ public function leftJoinWhere($table, $first, $operator, $second) } /** - * Add a subquery left join to the query. - * - * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query - * @param string $as - * @param \Closure|string $first - * @param string|null $operator - * @param string|null $second - * @return $this + * {@inheritdoc} */ public function leftJoinSub($query, $as, $first, $operator = null, $second = null) { @@ -568,13 +491,7 @@ public function leftJoinSub($query, $as, $first, $operator = null, $second = nul } /** - * Add a right join to the query. - * - * @param string $table - * @param \Closure|string $first - * @param string|null $operator - * @param string|null $second - * @return $this + * {@inheritdoc} */ public function rightJoin($table, $first, $operator = null, $second = null) { @@ -582,13 +499,7 @@ public function rightJoin($table, $first, $operator = null, $second = null) } /** - * Add a "right join where" clause to the query. - * - * @param string $table - * @param \Closure|string $first - * @param string $operator - * @param string $second - * @return $this + * {@inheritdoc} */ public function rightJoinWhere($table, $first, $operator, $second) { @@ -596,14 +507,7 @@ public function rightJoinWhere($table, $first, $operator, $second) } /** - * Add a subquery right join to the query. - * - * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder|string $query - * @param string $as - * @param \Closure|string $first - * @param string|null $operator - * @param string|null $second - * @return $this + * {@inheritdoc} */ public function rightJoinSub($query, $as, $first, $operator = null, $second = null) { @@ -611,13 +515,7 @@ public function rightJoinSub($query, $as, $first, $operator = null, $second = nu } /** - * Add a "cross join" clause to the query. - * - * @param string $table - * @param \Closure|string|null $first - * @param string|null $operator - * @param string|null $second - * @return $this + * {@inheritdoc} */ public function crossJoin($table, $first = null, $operator = null, $second = null) { @@ -631,11 +529,7 @@ public function crossJoin($table, $first = null, $operator = null, $second = nul } /** - * Add a subquery cross join to the query. - * - * @param \Closure|\Illuminate\Database\Query\Builder|string $query - * @param string $as - * @return $this + * {@inheritdoc} */ public function crossJoinSub($query, $as) { @@ -664,11 +558,7 @@ protected function newJoinClause(self $parentQuery, $type, $table) } /** - * Merge an array of where clauses and bindings. - * - * @param array $wheres - * @param array $bindings - * @return void + * {@inheritdoc} */ public function mergeWheres($wheres, $bindings) { @@ -682,13 +572,7 @@ public function mergeWheres($wheres, $bindings) } /** - * Add a basic where clause to the query. - * - * @param \Closure|string|array $column - * @param mixed $operator - * @param mixed $value - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function where($column, $operator = null, $value = null, $boolean = 'and') { @@ -793,14 +677,7 @@ protected function addArrayOfWheres($column, $boolean, $method = 'where') } /** - * Prepare the value and operator for a where clause. - * - * @param string $value - * @param string $operator - * @param bool $useDefault - * @return array - * - * @throws \InvalidArgumentException + * {@inheritdoc} */ public function prepareValueAndOperator($value, $operator, $useDefault = false) { @@ -841,13 +718,7 @@ protected function invalidOperator($operator) } /** - * Add an "or where" clause to the query. - * - * @param \Closure|string|array $column - * @param mixed $operator - * @param mixed $value - * @return $this - * {@inheritdoc} + * {@inheritdoc} */ public function orWhere($column, $operator = null, $value = null) { @@ -859,13 +730,7 @@ public function orWhere($column, $operator = null, $value = null) } /** - * Add a "where" clause comparing two columns to the query. - * - * @param string|array $first - * @param string|null $operator - * @param string|null $second - * @param string|null $boolean - * @return $this + * {@inheritdoc} */ public function whereColumn($first, $operator = null, $second = null, $boolean = 'and') { @@ -896,12 +761,7 @@ public function whereColumn($first, $operator = null, $second = null, $boolean = } /** - * Add an "or where" clause comparing two columns to the query. - * - * @param string|array $first - * @param string|null $operator - * @param string|null $second - * @return $this + * {@inheritdoc} */ public function orWhereColumn($first, $operator = null, $second = null) { @@ -909,12 +769,7 @@ public function orWhereColumn($first, $operator = null, $second = null) } /** - * Add a raw where clause to the query. - * - * @param string $sql - * @param mixed $bindings - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereRaw($sql, $bindings = [], $boolean = 'and') { @@ -926,11 +781,7 @@ public function whereRaw($sql, $bindings = [], $boolean = 'and') } /** - * Add a raw or where clause to the query. - * - * @param string $sql - * @param mixed $bindings - * @return $this + * {@inheritdoc} */ public function orWhereRaw($sql, $bindings = []) { @@ -938,13 +789,7 @@ public function orWhereRaw($sql, $bindings = []) } /** - * Add a "where in" clause to the query. - * - * @param string $column - * @param mixed $values - * @param string $boolean - * @param bool $not - * @return $this + * {@inheritdoc} */ public function whereIn($column, $values, $boolean = 'and', $not = false) { @@ -979,11 +824,7 @@ public function whereIn($column, $values, $boolean = 'and', $not = false) } /** - * Add an "or where in" clause to the query. - * - * @param string $column - * @param mixed $values - * @return $this + * {@inheritdoc} */ public function orWhereIn($column, $values) { @@ -991,12 +832,7 @@ public function orWhereIn($column, $values) } /** - * Add a "where not in" clause to the query. - * - * @param string $column - * @param mixed $values - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereNotIn($column, $values, $boolean = 'and') { @@ -1004,11 +840,7 @@ public function whereNotIn($column, $values, $boolean = 'and') } /** - * Add an "or where not in" clause to the query. - * - * @param string $column - * @param mixed $values - * @return $this + * {@inheritdoc} */ public function orWhereNotIn($column, $values) { @@ -1016,13 +848,7 @@ public function orWhereNotIn($column, $values) } /** - * Add a "where in raw" clause for integer values to the query. - * - * @param string $column - * @param \Illuminate\Contracts\Support\Arrayable|array $values - * @param string $boolean - * @param bool $not - * @return $this + * {@inheritdoc} */ public function whereIntegerInRaw($column, $values, $boolean = 'and', $not = false) { @@ -1042,11 +868,7 @@ public function whereIntegerInRaw($column, $values, $boolean = 'and', $not = fal } /** - * Add an "or where in raw" clause for integer values to the query. - * - * @param string $column - * @param \Illuminate\Contracts\Support\Arrayable|array $values - * @return $this + * {@inheritdoc} */ public function orWhereIntegerInRaw($column, $values) { @@ -1054,12 +876,7 @@ public function orWhereIntegerInRaw($column, $values) } /** - * Add a "where not in raw" clause for integer values to the query. - * - * @param string $column - * @param \Illuminate\Contracts\Support\Arrayable|array $values - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereIntegerNotInRaw($column, $values, $boolean = 'and') { @@ -1067,11 +884,7 @@ public function whereIntegerNotInRaw($column, $values, $boolean = 'and') } /** - * Add an "or where not in raw" clause for integer values to the query. - * - * @param string $column - * @param \Illuminate\Contracts\Support\Arrayable|array $values - * @return $this + * {@inheritdoc} */ public function orWhereIntegerNotInRaw($column, $values) { @@ -1079,12 +892,7 @@ public function orWhereIntegerNotInRaw($column, $values) } /** - * Add a "where null" clause to the query. - * - * @param string|array $columns - * @param string $boolean - * @param bool $not - * @return $this + * {@inheritdoc} */ public function whereNull($columns, $boolean = 'and', $not = false) { @@ -1098,10 +906,7 @@ public function whereNull($columns, $boolean = 'and', $not = false) } /** - * Add an "or where null" clause to the query. - * - * @param string|array $column - * @return $this + * {@inheritdoc} */ public function orWhereNull($column) { @@ -1109,11 +914,7 @@ public function orWhereNull($column) } /** - * Add a "where not null" clause to the query. - * - * @param string|array $columns - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereNotNull($columns, $boolean = 'and') { @@ -1121,13 +922,7 @@ public function whereNotNull($columns, $boolean = 'and') } /** - * Add a where between statement to the query. - * - * @param string|\Illuminate\Database\Query\Expression $column - * @param iterable $values - * @param string $boolean - * @param bool $not - * @return $this + * {@inheritdoc} */ public function whereBetween($column, iterable $values, $boolean = 'and', $not = false) { @@ -1145,13 +940,7 @@ public function whereBetween($column, iterable $values, $boolean = 'and', $not = } /** - * Add a where between statement using columns to the query. - * - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not - * @return $this + * {@inheritdoc} */ public function whereBetweenColumns($column, array $values, $boolean = 'and', $not = false) { @@ -1163,11 +952,7 @@ public function whereBetweenColumns($column, array $values, $boolean = 'and', $n } /** - * Add an or where between statement to the query. - * - * @param string $column - * @param iterable $values - * @return $this + * {@inheritdoc} */ public function orWhereBetween($column, iterable $values) { @@ -1175,11 +960,7 @@ public function orWhereBetween($column, iterable $values) } /** - * Add an or where between statement using columns to the query. - * - * @param string $column - * @param array $values - * @return $this + * {@inheritdoc} */ public function orWhereBetweenColumns($column, array $values) { @@ -1187,12 +968,7 @@ public function orWhereBetweenColumns($column, array $values) } /** - * Add a where not between statement to the query. - * - * @param string $column - * @param iterable $values - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereNotBetween($column, iterable $values, $boolean = 'and') { @@ -1200,12 +976,7 @@ public function whereNotBetween($column, iterable $values, $boolean = 'and') } /** - * Add a where not between statement using columns to the query. - * - * @param string $column - * @param array $values - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereNotBetweenColumns($column, array $values, $boolean = 'and') { @@ -1213,11 +984,7 @@ public function whereNotBetweenColumns($column, array $values, $boolean = 'and') } /** - * Add an or where not between statement to the query. - * - * @param string $column - * @param iterable $values - * @return $this + * {@inheritdoc} */ public function orWhereNotBetween($column, iterable $values) { @@ -1225,11 +992,7 @@ public function orWhereNotBetween($column, iterable $values) } /** - * Add an or where not between statement using columns to the query. - * - * @param string $column - * @param array $values - * @return $this + * {@inheritdoc} */ public function orWhereNotBetweenColumns($column, array $values) { @@ -1237,10 +1000,7 @@ public function orWhereNotBetweenColumns($column, array $values) } /** - * Add an "or where not null" clause to the query. - * - * @param string $column - * @return $this + * {@inheritdoc} */ public function orWhereNotNull($column) { @@ -1248,13 +1008,7 @@ public function orWhereNotNull($column) } /** - * Add a "where date" statement to the query. - * - * @param string $column - * @param string $operator - * @param \DateTimeInterface|string|null $value - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereDate($column, $operator, $value = null, $boolean = 'and') { @@ -1272,12 +1026,7 @@ public function whereDate($column, $operator, $value = null, $boolean = 'and') } /** - * Add an "or where date" statement to the query. - * - * @param string $column - * @param string $operator - * @param \DateTimeInterface|string|null $value - * @return $this + * {@inheritdoc} */ public function orWhereDate($column, $operator, $value = null) { @@ -1289,13 +1038,7 @@ public function orWhereDate($column, $operator, $value = null) } /** - * Add a "where time" statement to the query. - * - * @param string $column - * @param string $operator - * @param \DateTimeInterface|string|null $value - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereTime($column, $operator, $value = null, $boolean = 'and') { @@ -1313,12 +1056,7 @@ public function whereTime($column, $operator, $value = null, $boolean = 'and') } /** - * Add an "or where time" statement to the query. - * - * @param string $column - * @param string $operator - * @param \DateTimeInterface|string|null $value - * @return $this + * {@inheritdoc} */ public function orWhereTime($column, $operator, $value = null) { @@ -1330,13 +1068,7 @@ public function orWhereTime($column, $operator, $value = null) } /** - * Add a "where day" statement to the query. - * - * @param string $column - * @param string $operator - * @param \DateTimeInterface|string|null $value - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereDay($column, $operator, $value = null, $boolean = 'and') { @@ -1358,12 +1090,7 @@ public function whereDay($column, $operator, $value = null, $boolean = 'and') } /** - * Add an "or where day" statement to the query. - * - * @param string $column - * @param string $operator - * @param \DateTimeInterface|string|null $value - * @return $this + * {@inheritdoc} */ public function orWhereDay($column, $operator, $value = null) { @@ -1375,13 +1102,7 @@ public function orWhereDay($column, $operator, $value = null) } /** - * Add a "where month" statement to the query. - * - * @param string $column - * @param string $operator - * @param \DateTimeInterface|string|null $value - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereMonth($column, $operator, $value = null, $boolean = 'and') { @@ -1403,12 +1124,7 @@ public function whereMonth($column, $operator, $value = null, $boolean = 'and') } /** - * Add an "or where month" statement to the query. - * - * @param string $column - * @param string $operator - * @param \DateTimeInterface|string|null $value - * @return $this + * {@inheritdoc} */ public function orWhereMonth($column, $operator, $value = null) { @@ -1420,13 +1136,7 @@ public function orWhereMonth($column, $operator, $value = null) } /** - * Add a "where year" statement to the query. - * - * @param string $column - * @param string $operator - * @param \DateTimeInterface|string|int|null $value - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereYear($column, $operator, $value = null, $boolean = 'and') { @@ -1444,12 +1154,7 @@ public function whereYear($column, $operator, $value = null, $boolean = 'and') } /** - * Add an "or where year" statement to the query. - * - * @param string $column - * @param string $operator - * @param \DateTimeInterface|string|int|null $value - * @return $this + * {@inheritdoc} */ public function orWhereYear($column, $operator, $value = null) { @@ -1482,11 +1187,7 @@ protected function addDateBasedWhere($type, $column, $operator, $value, $boolean } /** - * Add a nested where statement to the query. - * - * @param \Closure $callback - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereNested(Closure $callback, $boolean = 'and') { @@ -1506,11 +1207,7 @@ public function forNestedWhere() } /** - * Add another query builder as a nested where to the query builder. - * - * @param \Illuminate\Database\Query\Builder $query - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function addNestedWhereQuery($query, $boolean = 'and') { @@ -1553,12 +1250,7 @@ protected function whereSub($column, $operator, Closure $callback, $boolean) } /** - * Add an exists clause to the query. - * - * @param \Closure $callback - * @param string $boolean - * @param bool $not - * @return $this + * {@inheritdoc} */ public function whereExists(Closure $callback, $boolean = 'and', $not = false) { @@ -1573,11 +1265,7 @@ public function whereExists(Closure $callback, $boolean = 'and', $not = false) } /** - * Add an or exists clause to the query. - * - * @param \Closure $callback - * @param bool $not - * @return $this + * {@inheritdoc} */ public function orWhereExists(Closure $callback, $not = false) { @@ -1585,11 +1273,7 @@ public function orWhereExists(Closure $callback, $not = false) } /** - * Add a where not exists clause to the query. - * - * @param \Closure $callback - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereNotExists(Closure $callback, $boolean = 'and') { @@ -1597,10 +1281,7 @@ public function whereNotExists(Closure $callback, $boolean = 'and') } /** - * Add a where not exists clause to the query. - * - * @param \Closure $callback - * @return $this + * {@inheritdoc} */ public function orWhereNotExists(Closure $callback) { @@ -1627,15 +1308,7 @@ public function addWhereExistsQuery(self $query, $boolean = 'and', $not = false) } /** - * Adds a where condition using row values. - * - * @param array $columns - * @param string $operator - * @param array $values - * @param string $boolean - * @return $this - * - * @throws \InvalidArgumentException + * {@inheritdoc} */ public function whereRowValues($columns, $operator, $values, $boolean = 'and') { @@ -1653,12 +1326,7 @@ public function whereRowValues($columns, $operator, $values, $boolean = 'and') } /** - * Adds an or where condition using row values. - * - * @param array $columns - * @param string $operator - * @param array $values - * @return $this + * {@inheritdoc} */ public function orWhereRowValues($columns, $operator, $values) { @@ -1666,13 +1334,7 @@ public function orWhereRowValues($columns, $operator, $values) } /** - * Add a "where JSON contains" clause to the query. - * - * @param string $column - * @param mixed $value - * @param string $boolean - * @param bool $not - * @return $this + * {@inheritdoc} */ public function whereJsonContains($column, $value, $boolean = 'and', $not = false) { @@ -1688,11 +1350,7 @@ public function whereJsonContains($column, $value, $boolean = 'and', $not = fals } /** - * Add an "or where JSON contains" clause to the query. - * - * @param string $column - * @param mixed $value - * @return $this + * {@inheritdoc} */ public function orWhereJsonContains($column, $value) { @@ -1700,12 +1358,7 @@ public function orWhereJsonContains($column, $value) } /** - * Add a "where JSON not contains" clause to the query. - * - * @param string $column - * @param mixed $value - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereJsonDoesntContain($column, $value, $boolean = 'and') { @@ -1713,11 +1366,7 @@ public function whereJsonDoesntContain($column, $value, $boolean = 'and') } /** - * Add an "or where JSON not contains" clause to the query. - * - * @param string $column - * @param mixed $value - * @return $this + * {@inheritdoc} */ public function orWhereJsonDoesntContain($column, $value) { @@ -1725,13 +1374,7 @@ public function orWhereJsonDoesntContain($column, $value) } /** - * Add a "where JSON length" clause to the query. - * - * @param string $column - * @param mixed $operator - * @param mixed $value - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function whereJsonLength($column, $operator, $value = null, $boolean = 'and') { @@ -1751,12 +1394,7 @@ public function whereJsonLength($column, $operator, $value = null, $boolean = 'a } /** - * Add an "or where JSON length" clause to the query. - * - * @param string $column - * @param mixed $operator - * @param mixed $value - * @return $this + * {@inheritdoc} */ public function orWhereJsonLength($column, $operator, $value = null) { @@ -1854,10 +1492,7 @@ public function orWhereFullText($columns, $value, array $options = []) } /** - * Add a "group by" clause to the query. - * - * @param array|string ...$groups - * @return $this + * {@inheritdoc} */ public function groupBy(...$groups) { @@ -1872,11 +1507,7 @@ public function groupBy(...$groups) } /** - * Add a raw groupBy clause to the query. - * - * @param string $sql - * @param array $bindings - * @return $this + * {@inheritdoc} */ public function groupByRaw($sql, array $bindings = []) { @@ -1888,13 +1519,7 @@ public function groupByRaw($sql, array $bindings = []) } /** - * Add a "having" clause to the query. - * - * @param string $column - * @param string|null $operator - * @param string|null $value - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function having($column, $operator = null, $value = null, $boolean = 'and') { @@ -1924,12 +1549,7 @@ public function having($column, $operator = null, $value = null, $boolean = 'and } /** - * Add an "or having" clause to the query. - * - * @param string $column - * @param string|null $operator - * @param string|null $value - * @return $this + * {@inheritdoc} */ public function orHaving($column, $operator = null, $value = null) { @@ -1941,12 +1561,7 @@ public function orHaving($column, $operator = null, $value = null) } /** - * Add a "having null" clause to the query. - * - * @param string|array $columns - * @param string $boolean - * @param bool $not - * @return $this + * {@inheritdoc} */ public function havingNull($columns, $boolean = 'and', $not = false) { @@ -1960,10 +1575,7 @@ public function havingNull($columns, $boolean = 'and', $not = false) } /** - * Add an "or having null" clause to the query. - * - * @param string $column - * @return $this + * {@inheritdoc} */ public function orHavingNull($column) { @@ -1971,11 +1583,7 @@ public function orHavingNull($column) } /** - * Add a "having not null" clause to the query. - * - * @param string|array $columns - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function havingNotNull($columns, $boolean = 'and') { @@ -1983,10 +1591,7 @@ public function havingNotNull($columns, $boolean = 'and') } /** - * Add an "or having not null" clause to the query. - * - * @param string $column - * @return $this + * {@inheritdoc} */ public function orHavingNotNull($column) { @@ -1994,13 +1599,7 @@ public function orHavingNotNull($column) } /** - * Add a "having between " clause to the query. - * - * @param string $column - * @param array $values - * @param string $boolean - * @param bool $not - * @return $this + * {@inheritdoc} */ public function havingBetween($column, array $values, $boolean = 'and', $not = false) { @@ -2014,12 +1613,7 @@ public function havingBetween($column, array $values, $boolean = 'and', $not = f } /** - * Add a raw having clause to the query. - * - * @param string $sql - * @param array $bindings - * @param string $boolean - * @return $this + * {@inheritdoc} */ public function havingRaw($sql, array $bindings = [], $boolean = 'and') { @@ -2033,11 +1627,7 @@ public function havingRaw($sql, array $bindings = [], $boolean = 'and') } /** - * Add a raw or having clause to the query. - * - * @param string $sql - * @param array $bindings - * @return $this + * {@inheritdoc} */ public function orHavingRaw($sql, array $bindings = []) { @@ -2045,13 +1635,7 @@ public function orHavingRaw($sql, array $bindings = []) } /** - * Add an "order by" clause to the query. - * - * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column - * @param string $direction - * @return $this - * - * @throws \InvalidArgumentException + * {@inheritdoc} */ public function orderBy($column, $direction = 'asc') { @@ -2078,10 +1662,7 @@ public function orderBy($column, $direction = 'asc') } /** - * Add a descending "order by" clause to the query. - * - * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column - * @return $this + * {@inheritdoc} */ public function orderByDesc($column) { @@ -2089,10 +1670,7 @@ public function orderByDesc($column) } /** - * Add an "order by" clause for a timestamp to the query. - * - * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column - * @return $this + * {@inheritdoc} */ public function latest($column = 'created_at') { @@ -2100,10 +1678,7 @@ public function latest($column = 'created_at') } /** - * Add an "order by" clause for a timestamp to the query. - * - * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column - * @return $this + * {@inheritdoc} */ public function oldest($column = 'created_at') { @@ -2111,10 +1686,7 @@ public function oldest($column = 'created_at') } /** - * Put the query's results in random order. - * - * @param string $seed - * @return $this + * {@inheritdoc} */ public function inRandomOrder($seed = '') { @@ -2122,11 +1694,7 @@ public function inRandomOrder($seed = '') } /** - * Add a raw "order by" clause to the query. - * - * @param string $sql - * @param array $bindings - * @return $this + * {@inheritdoc} */ public function orderByRaw($sql, $bindings = []) { @@ -2140,10 +1708,7 @@ public function orderByRaw($sql, $bindings = []) } /** - * Alias to set the "offset" value of the query. - * - * @param int $value - * @return $this + * {@inheritdoc} */ public function skip($value) { @@ -2151,10 +1716,7 @@ public function skip($value) } /** - * Set the "offset" value of the query. - * - * @param int $value - * @return $this + * {@inheritdoc} */ public function offset($value) { @@ -2166,10 +1728,7 @@ public function offset($value) } /** - * Alias to set the "limit" value of the query. - * - * @param int $value - * @return $this + * {@inheritdoc} */ public function take($value) { @@ -2177,10 +1736,7 @@ public function take($value) } /** - * Set the "limit" value of the query. - * - * @param int $value - * @return $this + * {@inheritdoc} */ public function limit($value) { @@ -2194,11 +1750,7 @@ public function limit($value) } /** - * Set the limit and offset for a given page. - * - * @param int $page - * @param int $perPage - * @return $this + * @inheritdoc */ public function forPage($page, $perPage = 15) { @@ -2206,12 +1758,7 @@ public function forPage($page, $perPage = 15) } /** - * Constrain the query to the previous "page" of results before a given ID. - * - * @param int $perPage - * @param int|null $lastId - * @param string $column - * @return $this + * @inheritdoc */ public function forPageBeforeId($perPage = 15, $lastId = 0, $column = 'id') { @@ -2226,12 +1773,7 @@ public function forPageBeforeId($perPage = 15, $lastId = 0, $column = 'id') } /** - * Constrain the query to the next "page" of results after a given ID. - * - * @param int $perPage - * @param int|null $lastId - * @param string $column - * @return $this + * @inheritdoc */ public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id') { @@ -2246,11 +1788,7 @@ public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id') } /** - * Remove all existing orders and optionally add a new order. - * - * @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string|null $column - * @param string $direction - * @return $this + * @inheritdoc */ public function reorder($column = null, $direction = 'asc') { @@ -2282,11 +1820,7 @@ protected function removeExistingOrdersFor($column) } /** - * Add a union statement to the query. - * - * @param \Illuminate\Database\Query\Builder|\Closure $query - * @param bool $all - * @return $this + * @inheritdoc */ public function union($query, $all = false) { @@ -2302,10 +1836,7 @@ public function union($query, $all = false) } /** - * Add a union all statement to the query. - * - * @param \Illuminate\Database\Query\Builder|\Closure $query - * @return $this + * @inheritdoc */ public function unionAll($query) { @@ -2313,10 +1844,7 @@ public function unionAll($query) } /** - * Lock the selected rows in the table. - * - * @param string|bool $value - * @return $this + * @inheritdoc */ public function lock($value = true) { @@ -2330,9 +1858,7 @@ public function lock($value = true) } /** - * Lock the selected rows in the table for updating. - * - * @return \Illuminate\Database\Query\Builder + * @inheritdoc */ public function lockForUpdate() { @@ -2340,9 +1866,7 @@ public function lockForUpdate() } /** - * Share lock the selected rows in the table. - * - * @return \Illuminate\Database\Query\Builder + * @inheritdoc */ public function sharedLock() { @@ -3258,9 +2782,7 @@ public function truncate() } /** - * Get a new instance of the query builder. - * - * @return \Illuminate\Database\Query\Builder + * @inheritdoc */ public function newQuery() { @@ -3278,10 +2800,7 @@ protected function forSubQuery() } /** - * Create a raw database expression. - * - * @param mixed $value - * @return \Illuminate\Database\Query\Expression + * @inheritdoc */ public function raw($value) { @@ -3289,9 +2808,7 @@ public function raw($value) } /** - * Get the current query value bindings in a flattened array. - * - * @return array + * @inheritdoc */ public function getBindings() { @@ -3299,9 +2816,7 @@ public function getBindings() } /** - * Get the raw array of bindings. - * - * @return array + * @inheritdoc */ public function getRawBindings() { @@ -3309,13 +2824,7 @@ public function getRawBindings() } /** - * Set the bindings on the query builder. - * - * @param array $bindings - * @param string $type - * @return $this - * - * @throws \InvalidArgumentException + * @inheritdoc */ public function setBindings(array $bindings, $type = 'where') { @@ -3329,13 +2838,7 @@ public function setBindings(array $bindings, $type = 'where') } /** - * Add a binding to the query. - * - * @param mixed $value - * @param string $type - * @return $this - * - * @throws \InvalidArgumentException + * @inheritdoc */ public function addBinding($value, $type = 'where') { @@ -3371,23 +2874,17 @@ public function castBinding($value) } /** - * Merge an array of bindings into our bindings. - * - * @param \Illuminate\Database\Query\Builder $query - * @return $this + * @inheritdoc */ - public function mergeBindings(self $query) + public function mergeBindings(BuilderContract $query) { - $this->bindings = array_merge_recursive($this->bindings, $query->bindings); + $this->bindings = array_merge_recursive($this->bindings, $query->getRawBindings()); return $this; } /** - * Remove all of the expressions from a list of bindings. - * - * @param array $bindings - * @return array + * @inheritdoc */ public function cleanBindings(array $bindings) { @@ -3422,9 +2919,7 @@ protected function defaultKeyName() } /** - * Get the database connection instance. - * - * @return \Illuminate\Database\ConnectionInterface + * @inheritdoc */ public function getConnection() { @@ -3432,9 +2927,7 @@ public function getConnection() } /** - * Get the database query processor instance. - * - * @return \Illuminate\Database\Query\Processors\Processor + * @inheritdoc */ public function getProcessor() { @@ -3442,9 +2935,7 @@ public function getProcessor() } /** - * Get the query grammar instance. - * - * @return \Illuminate\Database\Query\Grammars\Grammar + * @inheritdoc */ public function getGrammar() { @@ -3452,9 +2943,7 @@ public function getGrammar() } /** - * Use the write pdo for query. - * - * @return $this + * @inheritdoc */ public function useWritePdo() { @@ -3478,9 +2967,7 @@ protected function isQueryable($value) } /** - * Clone the query. - * - * @return static + * @inheritdoc */ public function clone() { @@ -3488,10 +2975,7 @@ public function clone() } /** - * Clone the query without the given properties. - * - * @param array $properties - * @return static + * @inheritdoc */ public function cloneWithout(array $properties) { @@ -3503,10 +2987,7 @@ public function cloneWithout(array $properties) } /** - * Clone the query without the given bindings. - * - * @param array $except - * @return static + * @inheritdoc */ public function cloneWithoutBindings(array $except) { From 800acf46edcf92962a6106119c5a5cf0c89cc64b Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Thu, 13 Jan 2022 20:13:51 -0500 Subject: [PATCH 3/5] Add test for #40400 --- tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index 3b6619990048..4b5c7413d348 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -471,6 +471,8 @@ public function testHasManyRelationshipCanBeSoftDeleted() $abigail = $abigail->fresh(); + $this->assertSame('select * from "posts" where "posts"."user_id" = ? and "posts"."user_id" is not null and "posts"."deleted_at" is null', $abigail->posts()->toSql()); + $this->assertCount(1, $abigail->posts); $this->assertSame('First Title', $abigail->posts->first()->title); $this->assertCount(1, $abigail->posts()->get()); From ade97f6a643c5d63a2b49c5bf8f61298f4dcaabf Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Fri, 14 Jan 2022 11:30:06 -0500 Subject: [PATCH 4/5] Add additional tests for global scopes on relations --- ...baseEloquentSoftDeletesIntegrationTest.php | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index 4b5c7413d348..9ac40a3963ef 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -52,6 +52,7 @@ public function createSchema() $table->increments('id'); $table->integer('user_id'); $table->string('title'); + $table->integer('priority'); $table->timestamps(); $table->softDeletes(); }); @@ -494,6 +495,97 @@ public function testHasManyRelationshipCanBeSoftDeleted() $this->assertCount(1, $abigail->posts()->withTrashed()->get()); } + public function testRelationExistsAndDoesntExistHonorsSoftDelete() + { + $this->createUsers(); + $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first(); + + // 'exists' should return true before soft delete + $abigail->posts()->create(['title' => 'First Title']); + $this->assertTrue($abigail->posts()->exists()); + $this->assertFalse($abigail->posts()->doesntExist()); + + // 'exists' should return false after soft delete + $abigail->posts()->first()->delete(); + $this->assertFalse($abigail->posts()->exists()); + $this->assertTrue($abigail->posts()->doesntExist()); + + // 'exists' should return true after restore + $abigail->posts()->withTrashed()->restore(); + $this->assertTrue($abigail->posts()->exists()); + $this->assertFalse($abigail->posts()->doesntExist()); + + // 'exists' should return false after a force delete + $abigail->posts()->first()->forceDelete(); + $this->assertFalse($abigail->posts()->exists()); + $this->assertTrue($abigail->posts()->doesntExist()); + } + + public function testRelationCountHonorsSoftDelete() + { + $this->createUsers(); + $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first(); + + // check count before soft delete + $abigail->posts()->create(['title' => 'First Title']); + $abigail->posts()->create(['title' => 'Second Title']); + $this->assertEquals(2, $abigail->posts()->count()); + + // check count after soft delete + $abigail->posts()->where('title', 'Second Title')->delete(); + $this->assertEquals(1, $abigail->posts()->count()); + + // check count after restore + $abigail->posts()->withTrashed()->restore(); + $this->assertEquals(2, $abigail->posts()->count()); + + // check count after a force delete + $abigail->posts()->where('title', 'Second Title')->forceDelete(); + $this->assertEquals(1, $abigail->posts()->count()); + } + + public function testRelationAggregatesHonorsSoftDelete() + { + $this->createUsers(); + $abigail = SoftDeletesTestUser::where('email', 'abigailotwell@gmail.com')->first(); + + // check aggregates before soft delete + $abigail->posts()->create(['title' => 'First Title', 'priority' => 2]); + $abigail->posts()->create(['title' => 'Second Title', 'priority' => 4]); + $abigail->posts()->create(['title' => 'Third Title', 'priority' => 6]); + $this->assertEquals(2, $abigail->posts()->min('priority')); + $this->assertEquals(6, $abigail->posts()->max('priority')); + $this->assertEquals(12, $abigail->posts()->sum('priority')); + $this->assertEquals(4, $abigail->posts()->avg('priority')); + + // check aggregates after soft delete + $abigail->posts()->where('title', 'First Title')->delete(); + $this->assertEquals(4, $abigail->posts()->min('priority')); + $this->assertEquals(6, $abigail->posts()->max('priority')); + $this->assertEquals(10, $abigail->posts()->sum('priority')); + $this->assertEquals(5, $abigail->posts()->avg('priority')); + + // check aggregates after restore + $abigail->posts()->withTrashed()->restore(); + $this->assertEquals(2, $abigail->posts()->min('priority')); + $this->assertEquals(6, $abigail->posts()->max('priority')); + $this->assertEquals(12, $abigail->posts()->sum('priority')); + $this->assertEquals(4, $abigail->posts()->avg('priority')); + + // check aggregates after a force delete + $abigail->posts()->where('title', 'Third Title')->forceDelete(); + $this->assertEquals(2, $abigail->posts()->min('priority')); + $this->assertEquals(4, $abigail->posts()->max('priority')); + $this->assertEquals(6, $abigail->posts()->sum('priority')); + $this->assertEquals(3, $abigail->posts()->avg('priority')); + } + + public function testSoftDeleteIsAppliedToNewQuery() + { + $query = (new SoftDeletesTestUser)->newQuery(); + $this->assertSame('select * from "users" where "users"."deleted_at" is null', $query->toSql()); + } + public function testSecondLevelRelationshipCanBeSoftDeleted() { $this->createUsers(); From eb5a3f603dbf58536b64e43144643a4a1c656fd6 Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Fri, 14 Jan 2022 11:33:49 -0500 Subject: [PATCH 5/5] Add default priority for tests --- tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index 9ac40a3963ef..19728bd46a33 100644 --- a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php +++ b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php @@ -52,7 +52,7 @@ public function createSchema() $table->increments('id'); $table->integer('user_id'); $table->string('title'); - $table->integer('priority'); + $table->integer('priority')->default(0); $table->timestamps(); $table->softDeletes(); });