Просмотр файла vendor/illuminate/database/Eloquent/Builder.php

Размер файла: 44.2Kb
  1. <?php
  2.  
  3. namespace Illuminate\Database\Eloquent;
  4.  
  5. use BadMethodCallException;
  6. use Closure;
  7. use Exception;
  8. use Illuminate\Contracts\Support\Arrayable;
  9. use Illuminate\Database\Concerns\BuildsQueries;
  10. use Illuminate\Database\Concerns\ExplainsQueries;
  11. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  12. use Illuminate\Database\Eloquent\Relations\Relation;
  13. use Illuminate\Database\Query\Builder as QueryBuilder;
  14. use Illuminate\Database\RecordsNotFoundException;
  15. use Illuminate\Pagination\Paginator;
  16. use Illuminate\Support\Arr;
  17. use Illuminate\Support\Str;
  18. use Illuminate\Support\Traits\ForwardsCalls;
  19. use ReflectionClass;
  20. use ReflectionMethod;
  21.  
  22. /**
  23. * @property-read HigherOrderBuilderProxy $orWhere
  24. *
  25. * @mixin \Illuminate\Database\Query\Builder
  26. */
  27. class Builder
  28. {
  29. use Concerns\QueriesRelationships, ExplainsQueries, ForwardsCalls;
  30. use BuildsQueries {
  31. sole as baseSole;
  32. }
  33.  
  34. /**
  35. * The base query builder instance.
  36. *
  37. * @var \Illuminate\Database\Query\Builder
  38. */
  39. protected $query;
  40.  
  41. /**
  42. * The model being queried.
  43. *
  44. * @var \Illuminate\Database\Eloquent\Model
  45. */
  46. protected $model;
  47.  
  48. /**
  49. * The relationships that should be eager loaded.
  50. *
  51. * @var array
  52. */
  53. protected $eagerLoad = [];
  54.  
  55. /**
  56. * All of the globally registered builder macros.
  57. *
  58. * @var array
  59. */
  60. protected static $macros = [];
  61.  
  62. /**
  63. * All of the locally registered builder macros.
  64. *
  65. * @var array
  66. */
  67. protected $localMacros = [];
  68.  
  69. /**
  70. * A replacement for the typical delete function.
  71. *
  72. * @var \Closure
  73. */
  74. protected $onDelete;
  75.  
  76. /**
  77. * The methods that should be returned from query builder.
  78. *
  79. * @var string[]
  80. */
  81. protected $passthru = [
  82. 'average',
  83. 'avg',
  84. 'count',
  85. 'dd',
  86. 'doesntExist',
  87. 'dump',
  88. 'exists',
  89. 'getBindings',
  90. 'getConnection',
  91. 'getGrammar',
  92. 'insert',
  93. 'insertGetId',
  94. 'insertOrIgnore',
  95. 'insertUsing',
  96. 'max',
  97. 'min',
  98. 'raw',
  99. 'sum',
  100. 'toSql',
  101. ];
  102.  
  103. /**
  104. * Applied global scopes.
  105. *
  106. * @var array
  107. */
  108. protected $scopes = [];
  109.  
  110. /**
  111. * Removed global scopes.
  112. *
  113. * @var array
  114. */
  115. protected $removedScopes = [];
  116.  
  117. /**
  118. * Create a new Eloquent query builder instance.
  119. *
  120. * @param \Illuminate\Database\Query\Builder $query
  121. * @return void
  122. */
  123. public function __construct(QueryBuilder $query)
  124. {
  125. $this->query = $query;
  126. }
  127.  
  128. /**
  129. * Create and return an un-saved model instance.
  130. *
  131. * @param array $attributes
  132. * @return \Illuminate\Database\Eloquent\Model|static
  133. */
  134. public function make(array $attributes = [])
  135. {
  136. return $this->newModelInstance($attributes);
  137. }
  138.  
  139. /**
  140. * Register a new global scope.
  141. *
  142. * @param string $identifier
  143. * @param \Illuminate\Database\Eloquent\Scope|\Closure $scope
  144. * @return $this
  145. */
  146. public function withGlobalScope($identifier, $scope)
  147. {
  148. $this->scopes[$identifier] = $scope;
  149.  
  150. if (method_exists($scope, 'extend')) {
  151. $scope->extend($this);
  152. }
  153.  
  154. return $this;
  155. }
  156.  
  157. /**
  158. * Remove a registered global scope.
  159. *
  160. * @param \Illuminate\Database\Eloquent\Scope|string $scope
  161. * @return $this
  162. */
  163. public function withoutGlobalScope($scope)
  164. {
  165. if (! is_string($scope)) {
  166. $scope = get_class($scope);
  167. }
  168.  
  169. unset($this->scopes[$scope]);
  170.  
  171. $this->removedScopes[] = $scope;
  172.  
  173. return $this;
  174. }
  175.  
  176. /**
  177. * Remove all or passed registered global scopes.
  178. *
  179. * @param array|null $scopes
  180. * @return $this
  181. */
  182. public function withoutGlobalScopes(array $scopes = null)
  183. {
  184. if (! is_array($scopes)) {
  185. $scopes = array_keys($this->scopes);
  186. }
  187.  
  188. foreach ($scopes as $scope) {
  189. $this->withoutGlobalScope($scope);
  190. }
  191.  
  192. return $this;
  193. }
  194.  
  195. /**
  196. * Get an array of global scopes that were removed from the query.
  197. *
  198. * @return array
  199. */
  200. public function removedScopes()
  201. {
  202. return $this->removedScopes;
  203. }
  204.  
  205. /**
  206. * Add a where clause on the primary key to the query.
  207. *
  208. * @param mixed $id
  209. * @return $this
  210. */
  211. public function whereKey($id)
  212. {
  213. if (is_array($id) || $id instanceof Arrayable) {
  214. $this->query->whereIn($this->model->getQualifiedKeyName(), $id);
  215.  
  216. return $this;
  217. }
  218.  
  219. if ($id !== null && $this->model->getKeyType() === 'string') {
  220. $id = (string) $id;
  221. }
  222.  
  223. return $this->where($this->model->getQualifiedKeyName(), '=', $id);
  224. }
  225.  
  226. /**
  227. * Add a where clause on the primary key to the query.
  228. *
  229. * @param mixed $id
  230. * @return $this
  231. */
  232. public function whereKeyNot($id)
  233. {
  234. if (is_array($id) || $id instanceof Arrayable) {
  235. $this->query->whereNotIn($this->model->getQualifiedKeyName(), $id);
  236.  
  237. return $this;
  238. }
  239.  
  240. if ($id !== null && $this->model->getKeyType() === 'string') {
  241. $id = (string) $id;
  242. }
  243.  
  244. return $this->where($this->model->getQualifiedKeyName(), '!=', $id);
  245. }
  246.  
  247. /**
  248. * Add a basic where clause to the query.
  249. *
  250. * @param \Closure|string|array|\Illuminate\Database\Query\Expression $column
  251. * @param mixed $operator
  252. * @param mixed $value
  253. * @param string $boolean
  254. * @return $this
  255. */
  256. public function where($column, $operator = null, $value = null, $boolean = 'and')
  257. {
  258. if ($column instanceof Closure && is_null($operator)) {
  259. $column($query = $this->model->newQueryWithoutRelationships());
  260.  
  261. $this->query->addNestedWhereQuery($query->getQuery(), $boolean);
  262. } else {
  263. $this->query->where(...func_get_args());
  264. }
  265.  
  266. return $this;
  267. }
  268.  
  269. /**
  270. * Add a basic where clause to the query, and return the first result.
  271. *
  272. * @param \Closure|string|array|\Illuminate\Database\Query\Expression $column
  273. * @param mixed $operator
  274. * @param mixed $value
  275. * @param string $boolean
  276. * @return \Illuminate\Database\Eloquent\Model|static
  277. */
  278. public function firstWhere($column, $operator = null, $value = null, $boolean = 'and')
  279. {
  280. return $this->where($column, $operator, $value, $boolean)->first();
  281. }
  282.  
  283. /**
  284. * Add an "or where" clause to the query.
  285. *
  286. * @param \Closure|array|string|\Illuminate\Database\Query\Expression $column
  287. * @param mixed $operator
  288. * @param mixed $value
  289. * @return $this
  290. */
  291. public function orWhere($column, $operator = null, $value = null)
  292. {
  293. [$value, $operator] = $this->query->prepareValueAndOperator(
  294. $value, $operator, func_num_args() === 2
  295. );
  296.  
  297. return $this->where($column, $operator, $value, 'or');
  298. }
  299.  
  300. /**
  301. * Add an "order by" clause for a timestamp to the query.
  302. *
  303. * @param string|\Illuminate\Database\Query\Expression $column
  304. * @return $this
  305. */
  306. public function latest($column = null)
  307. {
  308. if (is_null($column)) {
  309. $column = $this->model->getCreatedAtColumn() ?? 'created_at';
  310. }
  311.  
  312. $this->query->latest($column);
  313.  
  314. return $this;
  315. }
  316.  
  317. /**
  318. * Add an "order by" clause for a timestamp to the query.
  319. *
  320. * @param string|\Illuminate\Database\Query\Expression $column
  321. * @return $this
  322. */
  323. public function oldest($column = null)
  324. {
  325. if (is_null($column)) {
  326. $column = $this->model->getCreatedAtColumn() ?? 'created_at';
  327. }
  328.  
  329. $this->query->oldest($column);
  330.  
  331. return $this;
  332. }
  333.  
  334. /**
  335. * Create a collection of models from plain arrays.
  336. *
  337. * @param array $items
  338. * @return \Illuminate\Database\Eloquent\Collection
  339. */
  340. public function hydrate(array $items)
  341. {
  342. $instance = $this->newModelInstance();
  343.  
  344. return $instance->newCollection(array_map(function ($item) use ($instance) {
  345. return $instance->newFromBuilder($item);
  346. }, $items));
  347. }
  348.  
  349. /**
  350. * Create a collection of models from a raw query.
  351. *
  352. * @param string $query
  353. * @param array $bindings
  354. * @return \Illuminate\Database\Eloquent\Collection
  355. */
  356. public function fromQuery($query, $bindings = [])
  357. {
  358. return $this->hydrate(
  359. $this->query->getConnection()->select($query, $bindings)
  360. );
  361. }
  362.  
  363. /**
  364. * Find a model by its primary key.
  365. *
  366. * @param mixed $id
  367. * @param array $columns
  368. * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null
  369. */
  370. public function find($id, $columns = ['*'])
  371. {
  372. if (is_array($id) || $id instanceof Arrayable) {
  373. return $this->findMany($id, $columns);
  374. }
  375.  
  376. return $this->whereKey($id)->first($columns);
  377. }
  378.  
  379. /**
  380. * Find multiple models by their primary keys.
  381. *
  382. * @param \Illuminate\Contracts\Support\Arrayable|array $ids
  383. * @param array $columns
  384. * @return \Illuminate\Database\Eloquent\Collection
  385. */
  386. public function findMany($ids, $columns = ['*'])
  387. {
  388. $ids = $ids instanceof Arrayable ? $ids->toArray() : $ids;
  389.  
  390. if (empty($ids)) {
  391. return $this->model->newCollection();
  392. }
  393.  
  394. return $this->whereKey($ids)->get($columns);
  395. }
  396.  
  397. /**
  398. * Find a model by its primary key or throw an exception.
  399. *
  400. * @param mixed $id
  401. * @param array $columns
  402. * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static|static[]
  403. *
  404. * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  405. */
  406. public function findOrFail($id, $columns = ['*'])
  407. {
  408. $result = $this->find($id, $columns);
  409.  
  410. $id = $id instanceof Arrayable ? $id->toArray() : $id;
  411.  
  412. if (is_array($id)) {
  413. if (count($result) === count(array_unique($id))) {
  414. return $result;
  415. }
  416. } elseif (! is_null($result)) {
  417. return $result;
  418. }
  419.  
  420. throw (new ModelNotFoundException)->setModel(
  421. get_class($this->model), $id
  422. );
  423. }
  424.  
  425. /**
  426. * Find a model by its primary key or return fresh model instance.
  427. *
  428. * @param mixed $id
  429. * @param array $columns
  430. * @return \Illuminate\Database\Eloquent\Model|static
  431. */
  432. public function findOrNew($id, $columns = ['*'])
  433. {
  434. if (! is_null($model = $this->find($id, $columns))) {
  435. return $model;
  436. }
  437.  
  438. return $this->newModelInstance();
  439. }
  440.  
  441. /**
  442. * Get the first record matching the attributes or instantiate it.
  443. *
  444. * @param array $attributes
  445. * @param array $values
  446. * @return \Illuminate\Database\Eloquent\Model|static
  447. */
  448. public function firstOrNew(array $attributes = [], array $values = [])
  449. {
  450. if (! is_null($instance = $this->where($attributes)->first())) {
  451. return $instance;
  452. }
  453.  
  454. return $this->newModelInstance($attributes + $values);
  455. }
  456.  
  457. /**
  458. * Get the first record matching the attributes or create it.
  459. *
  460. * @param array $attributes
  461. * @param array $values
  462. * @return \Illuminate\Database\Eloquent\Model|static
  463. */
  464. public function firstOrCreate(array $attributes = [], array $values = [])
  465. {
  466. if (! is_null($instance = $this->where($attributes)->first())) {
  467. return $instance;
  468. }
  469.  
  470. return tap($this->newModelInstance($attributes + $values), function ($instance) {
  471. $instance->save();
  472. });
  473. }
  474.  
  475. /**
  476. * Create or update a record matching the attributes, and fill it with values.
  477. *
  478. * @param array $attributes
  479. * @param array $values
  480. * @return \Illuminate\Database\Eloquent\Model|static
  481. */
  482. public function updateOrCreate(array $attributes, array $values = [])
  483. {
  484. return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
  485. $instance->fill($values)->save();
  486. });
  487. }
  488.  
  489. /**
  490. * Execute the query and get the first result or throw an exception.
  491. *
  492. * @param array $columns
  493. * @return \Illuminate\Database\Eloquent\Model|static
  494. *
  495. * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  496. */
  497. public function firstOrFail($columns = ['*'])
  498. {
  499. if (! is_null($model = $this->first($columns))) {
  500. return $model;
  501. }
  502.  
  503. throw (new ModelNotFoundException)->setModel(get_class($this->model));
  504. }
  505.  
  506. /**
  507. * Execute the query and get the first result or call a callback.
  508. *
  509. * @param \Closure|array $columns
  510. * @param \Closure|null $callback
  511. * @return \Illuminate\Database\Eloquent\Model|static|mixed
  512. */
  513. public function firstOr($columns = ['*'], Closure $callback = null)
  514. {
  515. if ($columns instanceof Closure) {
  516. $callback = $columns;
  517.  
  518. $columns = ['*'];
  519. }
  520.  
  521. if (! is_null($model = $this->first($columns))) {
  522. return $model;
  523. }
  524.  
  525. return $callback();
  526. }
  527.  
  528. /**
  529. * Execute the query and get the first result if it's the sole matching record.
  530. *
  531. * @param array|string $columns
  532. * @return \Illuminate\Database\Eloquent\Model
  533. *
  534. * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  535. * @throws \Illuminate\Database\MultipleRecordsFoundException
  536. */
  537. public function sole($columns = ['*'])
  538. {
  539. try {
  540. return $this->baseSole($columns);
  541. } catch (RecordsNotFoundException $exception) {
  542. throw (new ModelNotFoundException)->setModel(get_class($this->model));
  543. }
  544. }
  545.  
  546. /**
  547. * Get a single column's value from the first result of a query.
  548. *
  549. * @param string|\Illuminate\Database\Query\Expression $column
  550. * @return mixed
  551. */
  552. public function value($column)
  553. {
  554. if ($result = $this->first([$column])) {
  555. return $result->{Str::afterLast($column, '.')};
  556. }
  557. }
  558.  
  559. /**
  560. * Execute the query as a "select" statement.
  561. *
  562. * @param array|string $columns
  563. * @return \Illuminate\Database\Eloquent\Collection|static[]
  564. */
  565. public function get($columns = ['*'])
  566. {
  567. $builder = $this->applyScopes();
  568.  
  569. // If we actually found models we will also eager load any relationships that
  570. // have been specified as needing to be eager loaded, which will solve the
  571. // n+1 query issue for the developers to avoid running a lot of queries.
  572. if (count($models = $builder->getModels($columns)) > 0) {
  573. $models = $builder->eagerLoadRelations($models);
  574. }
  575.  
  576. return $builder->getModel()->newCollection($models);
  577. }
  578.  
  579. /**
  580. * Get the hydrated models without eager loading.
  581. *
  582. * @param array|string $columns
  583. * @return \Illuminate\Database\Eloquent\Model[]|static[]
  584. */
  585. public function getModels($columns = ['*'])
  586. {
  587. return $this->model->hydrate(
  588. $this->query->get($columns)->all()
  589. )->all();
  590. }
  591.  
  592. /**
  593. * Eager load the relationships for the models.
  594. *
  595. * @param array $models
  596. * @return array
  597. */
  598. public function eagerLoadRelations(array $models)
  599. {
  600. foreach ($this->eagerLoad as $name => $constraints) {
  601. // For nested eager loads we'll skip loading them here and they will be set as an
  602. // eager load on the query to retrieve the relation so that they will be eager
  603. // loaded on that query, because that is where they get hydrated as models.
  604. if (strpos($name, '.') === false) {
  605. $models = $this->eagerLoadRelation($models, $name, $constraints);
  606. }
  607. }
  608.  
  609. return $models;
  610. }
  611.  
  612. /**
  613. * Eagerly load the relationship on a set of models.
  614. *
  615. * @param array $models
  616. * @param string $name
  617. * @param \Closure $constraints
  618. * @return array
  619. */
  620. protected function eagerLoadRelation(array $models, $name, Closure $constraints)
  621. {
  622. // First we will "back up" the existing where conditions on the query so we can
  623. // add our eager constraints. Then we will merge the wheres that were on the
  624. // query back to it in order that any where conditions might be specified.
  625. $relation = $this->getRelation($name);
  626.  
  627. $relation->addEagerConstraints($models);
  628.  
  629. $constraints($relation);
  630.  
  631. // Once we have the results, we just match those back up to their parent models
  632. // using the relationship instance. Then we just return the finished arrays
  633. // of models which have been eagerly hydrated and are readied for return.
  634. return $relation->match(
  635. $relation->initRelation($models, $name),
  636. $relation->getEager(), $name
  637. );
  638. }
  639.  
  640. /**
  641. * Get the relation instance for the given relation name.
  642. *
  643. * @param string $name
  644. * @return \Illuminate\Database\Eloquent\Relations\Relation
  645. */
  646. public function getRelation($name)
  647. {
  648. // We want to run a relationship query without any constrains so that we will
  649. // not have to remove these where clauses manually which gets really hacky
  650. // and error prone. We don't want constraints because we add eager ones.
  651. $relation = Relation::noConstraints(function () use ($name) {
  652. try {
  653. return $this->getModel()->newInstance()->$name();
  654. } catch (BadMethodCallException $e) {
  655. throw RelationNotFoundException::make($this->getModel(), $name);
  656. }
  657. });
  658.  
  659. $nested = $this->relationsNestedUnder($name);
  660.  
  661. // If there are nested relationships set on the query, we will put those onto
  662. // the query instances so that they can be handled after this relationship
  663. // is loaded. In this way they will all trickle down as they are loaded.
  664. if (count($nested) > 0) {
  665. $relation->getQuery()->with($nested);
  666. }
  667.  
  668. return $relation;
  669. }
  670.  
  671. /**
  672. * Get the deeply nested relations for a given top-level relation.
  673. *
  674. * @param string $relation
  675. * @return array
  676. */
  677. protected function relationsNestedUnder($relation)
  678. {
  679. $nested = [];
  680.  
  681. // We are basically looking for any relationships that are nested deeper than
  682. // the given top-level relationship. We will just check for any relations
  683. // that start with the given top relations and adds them to our arrays.
  684. foreach ($this->eagerLoad as $name => $constraints) {
  685. if ($this->isNestedUnder($relation, $name)) {
  686. $nested[substr($name, strlen($relation.'.'))] = $constraints;
  687. }
  688. }
  689.  
  690. return $nested;
  691. }
  692.  
  693. /**
  694. * Determine if the relationship is nested.
  695. *
  696. * @param string $relation
  697. * @param string $name
  698. * @return bool
  699. */
  700. protected function isNestedUnder($relation, $name)
  701. {
  702. return Str::contains($name, '.') && Str::startsWith($name, $relation.'.');
  703. }
  704.  
  705. /**
  706. * Get a lazy collection for the given query.
  707. *
  708. * @return \Illuminate\Support\LazyCollection
  709. */
  710. public function cursor()
  711. {
  712. return $this->applyScopes()->query->cursor()->map(function ($record) {
  713. return $this->newModelInstance()->newFromBuilder($record);
  714. });
  715. }
  716.  
  717. /**
  718. * Add a generic "order by" clause if the query doesn't already have one.
  719. *
  720. * @return void
  721. */
  722. protected function enforceOrderBy()
  723. {
  724. if (empty($this->query->orders) && empty($this->query->unionOrders)) {
  725. $this->orderBy($this->model->getQualifiedKeyName(), 'asc');
  726. }
  727. }
  728.  
  729. /**
  730. * Get an array with the values of a given column.
  731. *
  732. * @param string|\Illuminate\Database\Query\Expression $column
  733. * @param string|null $key
  734. * @return \Illuminate\Support\Collection
  735. */
  736. public function pluck($column, $key = null)
  737. {
  738. $results = $this->toBase()->pluck($column, $key);
  739.  
  740. // If the model has a mutator for the requested column, we will spin through
  741. // the results and mutate the values so that the mutated version of these
  742. // columns are returned as you would expect from these Eloquent models.
  743. if (! $this->model->hasGetMutator($column) &&
  744. ! $this->model->hasCast($column) &&
  745. ! in_array($column, $this->model->getDates())) {
  746. return $results;
  747. }
  748.  
  749. return $results->map(function ($value) use ($column) {
  750. return $this->model->newFromBuilder([$column => $value])->{$column};
  751. });
  752. }
  753.  
  754. /**
  755. * Paginate the given query.
  756. *
  757. * @param int|null $perPage
  758. * @param array $columns
  759. * @param string $pageName
  760. * @param int|null $page
  761. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  762. *
  763. * @throws \InvalidArgumentException
  764. */
  765. public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
  766. {
  767. $page = $page ?: Paginator::resolveCurrentPage($pageName);
  768.  
  769. $perPage = $perPage ?: $this->model->getPerPage();
  770.  
  771. $results = ($total = $this->toBase()->getCountForPagination())
  772. ? $this->forPage($page, $perPage)->get($columns)
  773. : $this->model->newCollection();
  774.  
  775. return $this->paginator($results, $total, $perPage, $page, [
  776. 'path' => Paginator::resolveCurrentPath(),
  777. 'pageName' => $pageName,
  778. ]);
  779. }
  780.  
  781. /**
  782. * Paginate the given query into a simple paginator.
  783. *
  784. * @param int|null $perPage
  785. * @param array $columns
  786. * @param string $pageName
  787. * @param int|null $page
  788. * @return \Illuminate\Contracts\Pagination\Paginator
  789. */
  790. public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
  791. {
  792. $page = $page ?: Paginator::resolveCurrentPage($pageName);
  793.  
  794. $perPage = $perPage ?: $this->model->getPerPage();
  795.  
  796. // Next we will set the limit and offset for this query so that when we get the
  797. // results we get the proper section of results. Then, we'll create the full
  798. // paginator instances for these results with the given page and per page.
  799. $this->skip(($page - 1) * $perPage)->take($perPage + 1);
  800.  
  801. return $this->simplePaginator($this->get($columns), $perPage, $page, [
  802. 'path' => Paginator::resolveCurrentPath(),
  803. 'pageName' => $pageName,
  804. ]);
  805. }
  806.  
  807. /**
  808. * Save a new model and return the instance.
  809. *
  810. * @param array $attributes
  811. * @return \Illuminate\Database\Eloquent\Model|$this
  812. */
  813. public function create(array $attributes = [])
  814. {
  815. return tap($this->newModelInstance($attributes), function ($instance) {
  816. $instance->save();
  817. });
  818. }
  819.  
  820. /**
  821. * Save a new model and return the instance. Allow mass-assignment.
  822. *
  823. * @param array $attributes
  824. * @return \Illuminate\Database\Eloquent\Model|$this
  825. */
  826. public function forceCreate(array $attributes)
  827. {
  828. return $this->model->unguarded(function () use ($attributes) {
  829. return $this->newModelInstance()->create($attributes);
  830. });
  831. }
  832.  
  833. /**
  834. * Update records in the database.
  835. *
  836. * @param array $values
  837. * @return int
  838. */
  839. public function update(array $values)
  840. {
  841. return $this->toBase()->update($this->addUpdatedAtColumn($values));
  842. }
  843.  
  844. /**
  845. * Insert new records or update the existing ones.
  846. *
  847. * @param array $values
  848. * @param array|string $uniqueBy
  849. * @param array|null $update
  850. * @return int
  851. */
  852. public function upsert(array $values, $uniqueBy, $update = null)
  853. {
  854. if (empty($values)) {
  855. return 0;
  856. }
  857.  
  858. if (! is_array(reset($values))) {
  859. $values = [$values];
  860. }
  861.  
  862. if (is_null($update)) {
  863. $update = array_keys(reset($values));
  864. }
  865.  
  866. return $this->toBase()->upsert(
  867. $this->addTimestampsToUpsertValues($values),
  868. $uniqueBy,
  869. $this->addUpdatedAtToUpsertColumns($update)
  870. );
  871. }
  872.  
  873. /**
  874. * Increment a column's value by a given amount.
  875. *
  876. * @param string|\Illuminate\Database\Query\Expression $column
  877. * @param float|int $amount
  878. * @param array $extra
  879. * @return int
  880. */
  881. public function increment($column, $amount = 1, array $extra = [])
  882. {
  883. return $this->toBase()->increment(
  884. $column, $amount, $this->addUpdatedAtColumn($extra)
  885. );
  886. }
  887.  
  888. /**
  889. * Decrement a column's value by a given amount.
  890. *
  891. * @param string|\Illuminate\Database\Query\Expression $column
  892. * @param float|int $amount
  893. * @param array $extra
  894. * @return int
  895. */
  896. public function decrement($column, $amount = 1, array $extra = [])
  897. {
  898. return $this->toBase()->decrement(
  899. $column, $amount, $this->addUpdatedAtColumn($extra)
  900. );
  901. }
  902.  
  903. /**
  904. * Add the "updated at" column to an array of values.
  905. *
  906. * @param array $values
  907. * @return array
  908. */
  909. protected function addUpdatedAtColumn(array $values)
  910. {
  911. if (! $this->model->usesTimestamps() ||
  912. is_null($this->model->getUpdatedAtColumn())) {
  913. return $values;
  914. }
  915.  
  916. $column = $this->model->getUpdatedAtColumn();
  917.  
  918. $values = array_merge(
  919. [$column => $this->model->freshTimestampString()],
  920. $values
  921. );
  922.  
  923. $segments = preg_split('/\s+as\s+/i', $this->query->from);
  924.  
  925. $qualifiedColumn = end($segments).'.'.$column;
  926.  
  927. $values[$qualifiedColumn] = $values[$column];
  928.  
  929. unset($values[$column]);
  930.  
  931. return $values;
  932. }
  933.  
  934. /**
  935. * Add timestamps to the inserted values.
  936. *
  937. * @param array $values
  938. * @return array
  939. */
  940. protected function addTimestampsToUpsertValues(array $values)
  941. {
  942. if (! $this->model->usesTimestamps()) {
  943. return $values;
  944. }
  945.  
  946. $timestamp = $this->model->freshTimestampString();
  947.  
  948. $columns = array_filter([
  949. $this->model->getCreatedAtColumn(),
  950. $this->model->getUpdatedAtColumn(),
  951. ]);
  952.  
  953. foreach ($columns as $column) {
  954. foreach ($values as &$row) {
  955. $row = array_merge([$column => $timestamp], $row);
  956. }
  957. }
  958.  
  959. return $values;
  960. }
  961.  
  962. /**
  963. * Add the "updated at" column to the updated columns.
  964. *
  965. * @param array $update
  966. * @return array
  967. */
  968. protected function addUpdatedAtToUpsertColumns(array $update)
  969. {
  970. if (! $this->model->usesTimestamps()) {
  971. return $update;
  972. }
  973.  
  974. $column = $this->model->getUpdatedAtColumn();
  975.  
  976. if (! is_null($column) &&
  977. ! array_key_exists($column, $update) &&
  978. ! in_array($column, $update)) {
  979. $update[] = $column;
  980. }
  981.  
  982. return $update;
  983. }
  984.  
  985. /**
  986. * Delete records from the database.
  987. *
  988. * @return mixed
  989. */
  990. public function delete()
  991. {
  992. if (isset($this->onDelete)) {
  993. return call_user_func($this->onDelete, $this);
  994. }
  995.  
  996. return $this->toBase()->delete();
  997. }
  998.  
  999. /**
  1000. * Run the default delete function on the builder.
  1001. *
  1002. * Since we do not apply scopes here, the row will actually be deleted.
  1003. *
  1004. * @return mixed
  1005. */
  1006. public function forceDelete()
  1007. {
  1008. return $this->query->delete();
  1009. }
  1010.  
  1011. /**
  1012. * Register a replacement for the default delete function.
  1013. *
  1014. * @param \Closure $callback
  1015. * @return void
  1016. */
  1017. public function onDelete(Closure $callback)
  1018. {
  1019. $this->onDelete = $callback;
  1020. }
  1021.  
  1022. /**
  1023. * Determine if the given model has a scope.
  1024. *
  1025. * @param string $scope
  1026. * @return bool
  1027. */
  1028. public function hasNamedScope($scope)
  1029. {
  1030. return $this->model && $this->model->hasNamedScope($scope);
  1031. }
  1032.  
  1033. /**
  1034. * Call the given local model scopes.
  1035. *
  1036. * @param array|string $scopes
  1037. * @return static|mixed
  1038. */
  1039. public function scopes($scopes)
  1040. {
  1041. $builder = $this;
  1042.  
  1043. foreach (Arr::wrap($scopes) as $scope => $parameters) {
  1044. // If the scope key is an integer, then the scope was passed as the value and
  1045. // the parameter list is empty, so we will format the scope name and these
  1046. // parameters here. Then, we'll be ready to call the scope on the model.
  1047. if (is_int($scope)) {
  1048. [$scope, $parameters] = [$parameters, []];
  1049. }
  1050.  
  1051. // Next we'll pass the scope callback to the callScope method which will take
  1052. // care of grouping the "wheres" properly so the logical order doesn't get
  1053. // messed up when adding scopes. Then we'll return back out the builder.
  1054. $builder = $builder->callNamedScope($scope, (array) $parameters);
  1055. }
  1056.  
  1057. return $builder;
  1058. }
  1059.  
  1060. /**
  1061. * Apply the scopes to the Eloquent builder instance and return it.
  1062. *
  1063. * @return static
  1064. */
  1065. public function applyScopes()
  1066. {
  1067. if (! $this->scopes) {
  1068. return $this;
  1069. }
  1070.  
  1071. $builder = clone $this;
  1072.  
  1073. foreach ($this->scopes as $identifier => $scope) {
  1074. if (! isset($builder->scopes[$identifier])) {
  1075. continue;
  1076. }
  1077.  
  1078. $builder->callScope(function (self $builder) use ($scope) {
  1079. // If the scope is a Closure we will just go ahead and call the scope with the
  1080. // builder instance. The "callScope" method will properly group the clauses
  1081. // that are added to this query so "where" clauses maintain proper logic.
  1082. if ($scope instanceof Closure) {
  1083. $scope($builder);
  1084. }
  1085.  
  1086. // If the scope is a scope object, we will call the apply method on this scope
  1087. // passing in the builder and the model instance. After we run all of these
  1088. // scopes we will return back the builder instance to the outside caller.
  1089. if ($scope instanceof Scope) {
  1090. $scope->apply($builder, $this->getModel());
  1091. }
  1092. });
  1093. }
  1094.  
  1095. return $builder;
  1096. }
  1097.  
  1098. /**
  1099. * Apply the given scope on the current builder instance.
  1100. *
  1101. * @param callable $scope
  1102. * @param array $parameters
  1103. * @return mixed
  1104. */
  1105. protected function callScope(callable $scope, array $parameters = [])
  1106. {
  1107. array_unshift($parameters, $this);
  1108.  
  1109. $query = $this->getQuery();
  1110.  
  1111. // We will keep track of how many wheres are on the query before running the
  1112. // scope so that we can properly group the added scope constraints in the
  1113. // query as their own isolated nested where statement and avoid issues.
  1114. $originalWhereCount = is_null($query->wheres)
  1115. ? 0 : count($query->wheres);
  1116.  
  1117. $result = $scope(...array_values($parameters)) ?? $this;
  1118.  
  1119. if (count((array) $query->wheres) > $originalWhereCount) {
  1120. $this->addNewWheresWithinGroup($query, $originalWhereCount);
  1121. }
  1122.  
  1123. return $result;
  1124. }
  1125.  
  1126. /**
  1127. * Apply the given named scope on the current builder instance.
  1128. *
  1129. * @param string $scope
  1130. * @param array $parameters
  1131. * @return mixed
  1132. */
  1133. protected function callNamedScope($scope, array $parameters = [])
  1134. {
  1135. return $this->callScope(function (...$parameters) use ($scope) {
  1136. return $this->model->callNamedScope($scope, $parameters);
  1137. }, $parameters);
  1138. }
  1139.  
  1140. /**
  1141. * Nest where conditions by slicing them at the given where count.
  1142. *
  1143. * @param \Illuminate\Database\Query\Builder $query
  1144. * @param int $originalWhereCount
  1145. * @return void
  1146. */
  1147. protected function addNewWheresWithinGroup(QueryBuilder $query, $originalWhereCount)
  1148. {
  1149. // Here, we totally remove all of the where clauses since we are going to
  1150. // rebuild them as nested queries by slicing the groups of wheres into
  1151. // their own sections. This is to prevent any confusing logic order.
  1152. $allWheres = $query->wheres;
  1153.  
  1154. $query->wheres = [];
  1155.  
  1156. $this->groupWhereSliceForScope(
  1157. $query, array_slice($allWheres, 0, $originalWhereCount)
  1158. );
  1159.  
  1160. $this->groupWhereSliceForScope(
  1161. $query, array_slice($allWheres, $originalWhereCount)
  1162. );
  1163. }
  1164.  
  1165. /**
  1166. * Slice where conditions at the given offset and add them to the query as a nested condition.
  1167. *
  1168. * @param \Illuminate\Database\Query\Builder $query
  1169. * @param array $whereSlice
  1170. * @return void
  1171. */
  1172. protected function groupWhereSliceForScope(QueryBuilder $query, $whereSlice)
  1173. {
  1174. $whereBooleans = collect($whereSlice)->pluck('boolean');
  1175.  
  1176. // Here we'll check if the given subset of where clauses contains any "or"
  1177. // booleans and in this case create a nested where expression. That way
  1178. // we don't add any unnecessary nesting thus keeping the query clean.
  1179. if ($whereBooleans->contains('or')) {
  1180. $query->wheres[] = $this->createNestedWhere(
  1181. $whereSlice, $whereBooleans->first()
  1182. );
  1183. } else {
  1184. $query->wheres = array_merge($query->wheres, $whereSlice);
  1185. }
  1186. }
  1187.  
  1188. /**
  1189. * Create a where array with nested where conditions.
  1190. *
  1191. * @param array $whereSlice
  1192. * @param string $boolean
  1193. * @return array
  1194. */
  1195. protected function createNestedWhere($whereSlice, $boolean = 'and')
  1196. {
  1197. $whereGroup = $this->getQuery()->forNestedWhere();
  1198.  
  1199. $whereGroup->wheres = $whereSlice;
  1200.  
  1201. return ['type' => 'Nested', 'query' => $whereGroup, 'boolean' => $boolean];
  1202. }
  1203.  
  1204. /**
  1205. * Set the relationships that should be eager loaded.
  1206. *
  1207. * @param string|array $relations
  1208. * @param string|\Closure|null $callback
  1209. * @return $this
  1210. */
  1211. public function with($relations, $callback = null)
  1212. {
  1213. if ($callback instanceof Closure) {
  1214. $eagerLoad = $this->parseWithRelations([$relations => $callback]);
  1215. } else {
  1216. $eagerLoad = $this->parseWithRelations(is_string($relations) ? func_get_args() : $relations);
  1217. }
  1218.  
  1219. $this->eagerLoad = array_merge($this->eagerLoad, $eagerLoad);
  1220.  
  1221. return $this;
  1222. }
  1223.  
  1224. /**
  1225. * Prevent the specified relations from being eager loaded.
  1226. *
  1227. * @param mixed $relations
  1228. * @return $this
  1229. */
  1230. public function without($relations)
  1231. {
  1232. $this->eagerLoad = array_diff_key($this->eagerLoad, array_flip(
  1233. is_string($relations) ? func_get_args() : $relations
  1234. ));
  1235.  
  1236. return $this;
  1237. }
  1238.  
  1239. /**
  1240. * Create a new instance of the model being queried.
  1241. *
  1242. * @param array $attributes
  1243. * @return \Illuminate\Database\Eloquent\Model|static
  1244. */
  1245. public function newModelInstance($attributes = [])
  1246. {
  1247. return $this->model->newInstance($attributes)->setConnection(
  1248. $this->query->getConnection()->getName()
  1249. );
  1250. }
  1251.  
  1252. /**
  1253. * Parse a list of relations into individuals.
  1254. *
  1255. * @param array $relations
  1256. * @return array
  1257. */
  1258. protected function parseWithRelations(array $relations)
  1259. {
  1260. $results = [];
  1261.  
  1262. foreach ($relations as $name => $constraints) {
  1263. // If the "name" value is a numeric key, we can assume that no constraints
  1264. // have been specified. We will just put an empty Closure there so that
  1265. // we can treat these all the same while we are looping through them.
  1266. if (is_numeric($name)) {
  1267. $name = $constraints;
  1268.  
  1269. [$name, $constraints] = Str::contains($name, ':')
  1270. ? $this->createSelectWithConstraint($name)
  1271. : [$name, static function () {
  1272. //
  1273. }];
  1274. }
  1275.  
  1276. // We need to separate out any nested includes, which allows the developers
  1277. // to load deep relationships using "dots" without stating each level of
  1278. // the relationship with its own key in the array of eager-load names.
  1279. $results = $this->addNestedWiths($name, $results);
  1280.  
  1281. $results[$name] = $constraints;
  1282. }
  1283.  
  1284. return $results;
  1285. }
  1286.  
  1287. /**
  1288. * Create a constraint to select the given columns for the relation.
  1289. *
  1290. * @param string $name
  1291. * @return array
  1292. */
  1293. protected function createSelectWithConstraint($name)
  1294. {
  1295. return [explode(':', $name)[0], static function ($query) use ($name) {
  1296. $query->select(array_map(static function ($column) use ($query) {
  1297. if (Str::contains($column, '.')) {
  1298. return $column;
  1299. }
  1300.  
  1301. return $query instanceof BelongsToMany
  1302. ? $query->getRelated()->getTable().'.'.$column
  1303. : $column;
  1304. }, explode(',', explode(':', $name)[1])));
  1305. }];
  1306. }
  1307.  
  1308. /**
  1309. * Parse the nested relationships in a relation.
  1310. *
  1311. * @param string $name
  1312. * @param array $results
  1313. * @return array
  1314. */
  1315. protected function addNestedWiths($name, $results)
  1316. {
  1317. $progress = [];
  1318.  
  1319. // If the relation has already been set on the result array, we will not set it
  1320. // again, since that would override any constraints that were already placed
  1321. // on the relationships. We will only set the ones that are not specified.
  1322. foreach (explode('.', $name) as $segment) {
  1323. $progress[] = $segment;
  1324.  
  1325. if (! isset($results[$last = implode('.', $progress)])) {
  1326. $results[$last] = static function () {
  1327. //
  1328. };
  1329. }
  1330. }
  1331.  
  1332. return $results;
  1333. }
  1334.  
  1335. /**
  1336. * Apply query-time casts to the model instance.
  1337. *
  1338. * @param array $casts
  1339. * @return $this
  1340. */
  1341. public function withCasts($casts)
  1342. {
  1343. $this->model->mergeCasts($casts);
  1344.  
  1345. return $this;
  1346. }
  1347.  
  1348. /**
  1349. * Get the underlying query builder instance.
  1350. *
  1351. * @return \Illuminate\Database\Query\Builder
  1352. */
  1353. public function getQuery()
  1354. {
  1355. return $this->query;
  1356. }
  1357.  
  1358. /**
  1359. * Set the underlying query builder instance.
  1360. *
  1361. * @param \Illuminate\Database\Query\Builder $query
  1362. * @return $this
  1363. */
  1364. public function setQuery($query)
  1365. {
  1366. $this->query = $query;
  1367.  
  1368. return $this;
  1369. }
  1370.  
  1371. /**
  1372. * Get a base query builder instance.
  1373. *
  1374. * @return \Illuminate\Database\Query\Builder
  1375. */
  1376. public function toBase()
  1377. {
  1378. return $this->applyScopes()->getQuery();
  1379. }
  1380.  
  1381. /**
  1382. * Get the relationships being eagerly loaded.
  1383. *
  1384. * @return array
  1385. */
  1386. public function getEagerLoads()
  1387. {
  1388. return $this->eagerLoad;
  1389. }
  1390.  
  1391. /**
  1392. * Set the relationships being eagerly loaded.
  1393. *
  1394. * @param array $eagerLoad
  1395. * @return $this
  1396. */
  1397. public function setEagerLoads(array $eagerLoad)
  1398. {
  1399. $this->eagerLoad = $eagerLoad;
  1400.  
  1401. return $this;
  1402. }
  1403.  
  1404. /**
  1405. * Get the default key name of the table.
  1406. *
  1407. * @return string
  1408. */
  1409. protected function defaultKeyName()
  1410. {
  1411. return $this->getModel()->getKeyName();
  1412. }
  1413.  
  1414. /**
  1415. * Get the model instance being queried.
  1416. *
  1417. * @return \Illuminate\Database\Eloquent\Model|static
  1418. */
  1419. public function getModel()
  1420. {
  1421. return $this->model;
  1422. }
  1423.  
  1424. /**
  1425. * Set a model instance for the model being queried.
  1426. *
  1427. * @param \Illuminate\Database\Eloquent\Model $model
  1428. * @return $this
  1429. */
  1430. public function setModel(Model $model)
  1431. {
  1432. $this->model = $model;
  1433.  
  1434. $this->query->from($model->getTable());
  1435.  
  1436. return $this;
  1437. }
  1438.  
  1439. /**
  1440. * Qualify the given column name by the model's table.
  1441. *
  1442. * @param string|\Illuminate\Database\Query\Expression $column
  1443. * @return string
  1444. */
  1445. public function qualifyColumn($column)
  1446. {
  1447. return $this->model->qualifyColumn($column);
  1448. }
  1449.  
  1450. /**
  1451. * Get the given macro by name.
  1452. *
  1453. * @param string $name
  1454. * @return \Closure
  1455. */
  1456. public function getMacro($name)
  1457. {
  1458. return Arr::get($this->localMacros, $name);
  1459. }
  1460.  
  1461. /**
  1462. * Checks if a macro is registered.
  1463. *
  1464. * @param string $name
  1465. * @return bool
  1466. */
  1467. public function hasMacro($name)
  1468. {
  1469. return isset($this->localMacros[$name]);
  1470. }
  1471.  
  1472. /**
  1473. * Get the given global macro by name.
  1474. *
  1475. * @param string $name
  1476. * @return \Closure
  1477. */
  1478. public static function getGlobalMacro($name)
  1479. {
  1480. return Arr::get(static::$macros, $name);
  1481. }
  1482.  
  1483. /**
  1484. * Checks if a global macro is registered.
  1485. *
  1486. * @param string $name
  1487. * @return bool
  1488. */
  1489. public static function hasGlobalMacro($name)
  1490. {
  1491. return isset(static::$macros[$name]);
  1492. }
  1493.  
  1494. /**
  1495. * Dynamically access builder proxies.
  1496. *
  1497. * @param string $key
  1498. * @return mixed
  1499. *
  1500. * @throws \Exception
  1501. */
  1502. public function __get($key)
  1503. {
  1504. if ($key === 'orWhere') {
  1505. return new HigherOrderBuilderProxy($this, $key);
  1506. }
  1507.  
  1508. throw new Exception("Property [{$key}] does not exist on the Eloquent builder instance.");
  1509. }
  1510.  
  1511. /**
  1512. * Dynamically handle calls into the query instance.
  1513. *
  1514. * @param string $method
  1515. * @param array $parameters
  1516. * @return mixed
  1517. */
  1518. public function __call($method, $parameters)
  1519. {
  1520. if ($method === 'macro') {
  1521. $this->localMacros[$parameters[0]] = $parameters[1];
  1522.  
  1523. return;
  1524. }
  1525.  
  1526. if ($this->hasMacro($method)) {
  1527. array_unshift($parameters, $this);
  1528.  
  1529. return $this->localMacros[$method](...$parameters);
  1530. }
  1531.  
  1532. if (static::hasGlobalMacro($method)) {
  1533. $callable = static::$macros[$method];
  1534.  
  1535. if ($callable instanceof Closure) {
  1536. $callable = $callable->bindTo($this, static::class);
  1537. }
  1538.  
  1539. return $callable(...$parameters);
  1540. }
  1541.  
  1542. if ($this->hasNamedScope($method)) {
  1543. return $this->callNamedScope($method, $parameters);
  1544. }
  1545.  
  1546. if (in_array($method, $this->passthru)) {
  1547. return $this->toBase()->{$method}(...$parameters);
  1548. }
  1549.  
  1550. $this->forwardCallTo($this->query, $method, $parameters);
  1551.  
  1552. return $this;
  1553. }
  1554.  
  1555. /**
  1556. * Dynamically handle calls into the query instance.
  1557. *
  1558. * @param string $method
  1559. * @param array $parameters
  1560. * @return mixed
  1561. *
  1562. * @throws \BadMethodCallException
  1563. */
  1564. public static function __callStatic($method, $parameters)
  1565. {
  1566. if ($method === 'macro') {
  1567. static::$macros[$parameters[0]] = $parameters[1];
  1568.  
  1569. return;
  1570. }
  1571.  
  1572. if ($method === 'mixin') {
  1573. return static::registerMixin($parameters[0], $parameters[1] ?? true);
  1574. }
  1575.  
  1576. if (! static::hasGlobalMacro($method)) {
  1577. static::throwBadMethodCallException($method);
  1578. }
  1579.  
  1580. $callable = static::$macros[$method];
  1581.  
  1582. if ($callable instanceof Closure) {
  1583. $callable = $callable->bindTo(null, static::class);
  1584. }
  1585.  
  1586. return $callable(...$parameters);
  1587. }
  1588.  
  1589. /**
  1590. * Register the given mixin with the builder.
  1591. *
  1592. * @param string $mixin
  1593. * @param bool $replace
  1594. * @return void
  1595. */
  1596. protected static function registerMixin($mixin, $replace)
  1597. {
  1598. $methods = (new ReflectionClass($mixin))->getMethods(
  1599. ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED
  1600. );
  1601.  
  1602. foreach ($methods as $method) {
  1603. if ($replace || ! static::hasGlobalMacro($method->name)) {
  1604. $method->setAccessible(true);
  1605.  
  1606. static::macro($method->name, $method->invoke($mixin));
  1607. }
  1608. }
  1609. }
  1610.  
  1611. /**
  1612. * Force a clone of the underlying query builder when cloning.
  1613. *
  1614. * @return void
  1615. */
  1616. public function __clone()
  1617. {
  1618. $this->query = clone $this->query;
  1619. }
  1620. }