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 a45f5f3c9b06..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; } @@ -314,7 +316,7 @@ public function getBaseQuery() */ public function toBase() { - return $this->query->getQuery(); + return $this->query->toBase(); } /** diff --git a/src/Illuminate/Database/Query/Builder.php b/src/Illuminate/Database/Query/Builder.php index 07569c1f96b5..afb819b734ac 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,12 +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} */ public function orWhere($column, $operator = null, $value = null) { @@ -858,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') { @@ -895,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) { @@ -908,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') { @@ -925,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 = []) { @@ -937,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) { @@ -978,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) { @@ -990,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') { @@ -1003,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) { @@ -1015,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) { @@ -1041,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) { @@ -1053,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') { @@ -1066,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) { @@ -1078,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) { @@ -1097,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) { @@ -1108,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') { @@ -1120,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) { @@ -1144,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) { @@ -1162,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) { @@ -1174,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) { @@ -1186,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') { @@ -1199,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') { @@ -1212,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) { @@ -1224,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) { @@ -1236,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) { @@ -1247,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') { @@ -1271,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) { @@ -1288,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') { @@ -1312,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) { @@ -1329,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') { @@ -1357,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) { @@ -1374,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') { @@ -1402,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) { @@ -1419,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') { @@ -1443,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) { @@ -1481,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') { @@ -1505,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') { @@ -1552,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) { @@ -1572,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) { @@ -1584,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') { @@ -1596,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) { @@ -1626,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') { @@ -1652,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) { @@ -1665,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) { @@ -1687,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) { @@ -1699,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') { @@ -1712,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) { @@ -1724,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') { @@ -1750,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) { @@ -1862,10 +1501,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) { @@ -1880,11 +1516,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 = []) { @@ -1896,13 +1528,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') { @@ -1932,12 +1558,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) { @@ -1949,12 +1570,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) { @@ -1968,10 +1584,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) { @@ -1979,11 +1592,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') { @@ -1991,10 +1600,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) { @@ -2002,13 +1608,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) { @@ -2022,12 +1622,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') { @@ -2041,11 +1636,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 = []) { @@ -2053,13 +1644,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') { @@ -2086,10 +1671,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) { @@ -2097,10 +1679,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') { @@ -2108,10 +1687,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') { @@ -2119,10 +1695,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 = '') { @@ -2130,11 +1703,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 = []) { @@ -2148,10 +1717,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) { @@ -2159,10 +1725,7 @@ public function skip($value) } /** - * Set the "offset" value of the query. - * - * @param int $value - * @return $this + * {@inheritdoc} */ public function offset($value) { @@ -2174,10 +1737,7 @@ public function offset($value) } /** - * Alias to set the "limit" value of the query. - * - * @param int $value - * @return $this + * {@inheritdoc} */ public function take($value) { @@ -2185,10 +1745,7 @@ public function take($value) } /** - * Set the "limit" value of the query. - * - * @param int $value - * @return $this + * {@inheritdoc} */ public function limit($value) { @@ -2202,11 +1759,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) { @@ -2214,12 +1767,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') { @@ -2234,12 +1782,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') { @@ -2254,11 +1797,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') { @@ -2290,11 +1829,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) { @@ -2310,10 +1845,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) { @@ -2321,10 +1853,7 @@ public function unionAll($query) } /** - * Lock the selected rows in the table. - * - * @param string|bool $value - * @return $this + * @inheritdoc */ public function lock($value = true) { @@ -2338,9 +1867,7 @@ public function lock($value = true) } /** - * Lock the selected rows in the table for updating. - * - * @return \Illuminate\Database\Query\Builder + * @inheritdoc */ public function lockForUpdate() { @@ -2348,9 +1875,7 @@ public function lockForUpdate() } /** - * Share lock the selected rows in the table. - * - * @return \Illuminate\Database\Query\Builder + * @inheritdoc */ public function sharedLock() { @@ -3266,9 +2791,7 @@ public function truncate() } /** - * Get a new instance of the query builder. - * - * @return \Illuminate\Database\Query\Builder + * @inheritdoc */ public function newQuery() { @@ -3286,10 +2809,7 @@ protected function forSubQuery() } /** - * Create a raw database expression. - * - * @param mixed $value - * @return \Illuminate\Database\Query\Expression + * @inheritdoc */ public function raw($value) { @@ -3297,9 +2817,7 @@ public function raw($value) } /** - * Get the current query value bindings in a flattened array. - * - * @return array + * @inheritdoc */ public function getBindings() { @@ -3307,9 +2825,7 @@ public function getBindings() } /** - * Get the raw array of bindings. - * - * @return array + * @inheritdoc */ public function getRawBindings() { @@ -3317,13 +2833,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') { @@ -3337,13 +2847,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') { @@ -3379,23 +2883,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) { @@ -3430,9 +2928,7 @@ protected function defaultKeyName() } /** - * Get the database connection instance. - * - * @return \Illuminate\Database\ConnectionInterface + * @inheritdoc */ public function getConnection() { @@ -3440,9 +2936,7 @@ public function getConnection() } /** - * Get the database query processor instance. - * - * @return \Illuminate\Database\Query\Processors\Processor + * @inheritdoc */ public function getProcessor() { @@ -3450,9 +2944,7 @@ public function getProcessor() } /** - * Get the query grammar instance. - * - * @return \Illuminate\Database\Query\Grammars\Grammar + * @inheritdoc */ public function getGrammar() { @@ -3460,9 +2952,7 @@ public function getGrammar() } /** - * Use the write pdo for query. - * - * @return $this + * @inheritdoc */ public function useWritePdo() { @@ -3486,9 +2976,7 @@ protected function isQueryable($value) } /** - * Clone the query. - * - * @return static + * @inheritdoc */ public function clone() { @@ -3496,10 +2984,7 @@ public function clone() } /** - * Clone the query without the given properties. - * - * @param array $properties - * @return static + * @inheritdoc */ public function cloneWithout(array $properties) { @@ -3511,10 +2996,7 @@ public function cloneWithout(array $properties) } /** - * Clone the query without the given bindings. - * - * @param array $except - * @return static + * @inheritdoc */ public function cloneWithoutBindings(array $except) { diff --git a/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php b/tests/Database/DatabaseEloquentSoftDeletesIntegrationTest.php index 786c9c35242b..19728bd46a33 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')->default(0); $table->timestamps(); $table->softDeletes(); }); @@ -471,8 +472,11 @@ 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()); $this->assertCount(2, $abigail->posts()->withTrashed()->get()); // restore @@ -491,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(); @@ -504,6 +599,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()); }