Builder.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. <?php
  2. namespace Illuminate\Database\Eloquent;
  3. use Closure;
  4. use Exception;
  5. use BadMethodCallException;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Str;
  8. use Illuminate\Pagination\Paginator;
  9. use Illuminate\Contracts\Support\Arrayable;
  10. use Illuminate\Support\Traits\ForwardsCalls;
  11. use Illuminate\Database\Concerns\BuildsQueries;
  12. use Illuminate\Database\Eloquent\Relations\Relation;
  13. use Illuminate\Database\Query\Builder as QueryBuilder;
  14. /**
  15. * @property-read HigherOrderBuilderProxy $orWhere
  16. *
  17. * @mixin \Illuminate\Database\Query\Builder
  18. */
  19. class Builder
  20. {
  21. use BuildsQueries, Concerns\QueriesRelationships, ForwardsCalls;
  22. /**
  23. * The base query builder instance.
  24. *
  25. * @var \Illuminate\Database\Query\Builder
  26. */
  27. protected $query;
  28. /**
  29. * The model being queried.
  30. *
  31. * @var \Illuminate\Database\Eloquent\Model
  32. */
  33. protected $model;
  34. /**
  35. * The relationships that should be eager loaded.
  36. *
  37. * @var array
  38. */
  39. protected $eagerLoad = [];
  40. /**
  41. * All of the globally registered builder macros.
  42. *
  43. * @var array
  44. */
  45. protected static $macros = [];
  46. /**
  47. * All of the locally registered builder macros.
  48. *
  49. * @var array
  50. */
  51. protected $localMacros = [];
  52. /**
  53. * A replacement for the typical delete function.
  54. *
  55. * @var \Closure
  56. */
  57. protected $onDelete;
  58. /**
  59. * The methods that should be returned from query builder.
  60. *
  61. * @var array
  62. */
  63. protected $passthru = [
  64. 'insert', 'insertOrIgnore', 'insertGetId', 'insertUsing', 'getBindings', 'toSql', 'dump', 'dd',
  65. 'exists', 'doesntExist', 'count', 'min', 'max', 'avg', 'average', 'sum', 'getConnection',
  66. ];
  67. /**
  68. * Applied global scopes.
  69. *
  70. * @var array
  71. */
  72. protected $scopes = [];
  73. /**
  74. * Removed global scopes.
  75. *
  76. * @var array
  77. */
  78. protected $removedScopes = [];
  79. /**
  80. * Create a new Eloquent query builder instance.
  81. *
  82. * @param \Illuminate\Database\Query\Builder $query
  83. * @return void
  84. */
  85. public function __construct(QueryBuilder $query)
  86. {
  87. $this->query = $query;
  88. }
  89. /**
  90. * Create and return an un-saved model instance.
  91. *
  92. * @param array $attributes
  93. * @return \Illuminate\Database\Eloquent\Model
  94. */
  95. public function make(array $attributes = [])
  96. {
  97. return $this->newModelInstance($attributes);
  98. }
  99. /**
  100. * Register a new global scope.
  101. *
  102. * @param string $identifier
  103. * @param \Illuminate\Database\Eloquent\Scope|\Closure $scope
  104. * @return $this
  105. */
  106. public function withGlobalScope($identifier, $scope)
  107. {
  108. $this->scopes[$identifier] = $scope;
  109. if (method_exists($scope, 'extend')) {
  110. $scope->extend($this);
  111. }
  112. return $this;
  113. }
  114. /**
  115. * Remove a registered global scope.
  116. *
  117. * @param \Illuminate\Database\Eloquent\Scope|string $scope
  118. * @return $this
  119. */
  120. public function withoutGlobalScope($scope)
  121. {
  122. if (! is_string($scope)) {
  123. $scope = get_class($scope);
  124. }
  125. unset($this->scopes[$scope]);
  126. $this->removedScopes[] = $scope;
  127. return $this;
  128. }
  129. /**
  130. * Remove all or passed registered global scopes.
  131. *
  132. * @param array|null $scopes
  133. * @return $this
  134. */
  135. public function withoutGlobalScopes(array $scopes = null)
  136. {
  137. if (! is_array($scopes)) {
  138. $scopes = array_keys($this->scopes);
  139. }
  140. foreach ($scopes as $scope) {
  141. $this->withoutGlobalScope($scope);
  142. }
  143. return $this;
  144. }
  145. /**
  146. * Get an array of global scopes that were removed from the query.
  147. *
  148. * @return array
  149. */
  150. public function removedScopes()
  151. {
  152. return $this->removedScopes;
  153. }
  154. /**
  155. * Add a where clause on the primary key to the query.
  156. *
  157. * @param mixed $id
  158. * @return $this
  159. */
  160. public function whereKey($id)
  161. {
  162. if (is_array($id) || $id instanceof Arrayable) {
  163. $this->query->whereIn($this->model->getQualifiedKeyName(), $id);
  164. return $this;
  165. }
  166. return $this->where($this->model->getQualifiedKeyName(), '=', $id);
  167. }
  168. /**
  169. * Add a where clause on the primary key to the query.
  170. *
  171. * @param mixed $id
  172. * @return $this
  173. */
  174. public function whereKeyNot($id)
  175. {
  176. if (is_array($id) || $id instanceof Arrayable) {
  177. $this->query->whereNotIn($this->model->getQualifiedKeyName(), $id);
  178. return $this;
  179. }
  180. return $this->where($this->model->getQualifiedKeyName(), '!=', $id);
  181. }
  182. /**
  183. * Add a basic where clause to the query.
  184. *
  185. * @param string|array|\Closure $column
  186. * @param mixed $operator
  187. * @param mixed $value
  188. * @param string $boolean
  189. * @return $this
  190. */
  191. public function where($column, $operator = null, $value = null, $boolean = 'and')
  192. {
  193. if ($column instanceof Closure) {
  194. $column($query = $this->model->newModelQuery());
  195. $this->query->addNestedWhereQuery($query->getQuery(), $boolean);
  196. } else {
  197. $this->query->where(...func_get_args());
  198. }
  199. return $this;
  200. }
  201. /**
  202. * Add an "or where" clause to the query.
  203. *
  204. * @param \Closure|array|string $column
  205. * @param mixed $operator
  206. * @param mixed $value
  207. * @return \Illuminate\Database\Eloquent\Builder|static
  208. */
  209. public function orWhere($column, $operator = null, $value = null)
  210. {
  211. [$value, $operator] = $this->query->prepareValueAndOperator(
  212. $value, $operator, func_num_args() === 2
  213. );
  214. return $this->where($column, $operator, $value, 'or');
  215. }
  216. /**
  217. * Add an "order by" clause for a timestamp to the query.
  218. *
  219. * @param string $column
  220. * @return $this
  221. */
  222. public function latest($column = null)
  223. {
  224. if (is_null($column)) {
  225. $column = $this->model->getCreatedAtColumn() ?? 'created_at';
  226. }
  227. $this->query->latest($column);
  228. return $this;
  229. }
  230. /**
  231. * Add an "order by" clause for a timestamp to the query.
  232. *
  233. * @param string $column
  234. * @return $this
  235. */
  236. public function oldest($column = null)
  237. {
  238. if (is_null($column)) {
  239. $column = $this->model->getCreatedAtColumn() ?? 'created_at';
  240. }
  241. $this->query->oldest($column);
  242. return $this;
  243. }
  244. /**
  245. * Create a collection of models from plain arrays.
  246. *
  247. * @param array $items
  248. * @return \Illuminate\Database\Eloquent\Collection
  249. */
  250. public function hydrate(array $items)
  251. {
  252. $instance = $this->newModelInstance();
  253. return $instance->newCollection(array_map(function ($item) use ($instance) {
  254. return $instance->newFromBuilder($item);
  255. }, $items));
  256. }
  257. /**
  258. * Create a collection of models from a raw query.
  259. *
  260. * @param string $query
  261. * @param array $bindings
  262. * @return \Illuminate\Database\Eloquent\Collection
  263. */
  264. public function fromQuery($query, $bindings = [])
  265. {
  266. return $this->hydrate(
  267. $this->query->getConnection()->select($query, $bindings)
  268. );
  269. }
  270. /**
  271. * Find a model by its primary key.
  272. *
  273. * @param mixed $id
  274. * @param array $columns
  275. * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null
  276. */
  277. public function find($id, $columns = ['*'])
  278. {
  279. if (is_array($id) || $id instanceof Arrayable) {
  280. return $this->findMany($id, $columns);
  281. }
  282. return $this->whereKey($id)->first($columns);
  283. }
  284. /**
  285. * Find multiple models by their primary keys.
  286. *
  287. * @param \Illuminate\Contracts\Support\Arrayable|array $ids
  288. * @param array $columns
  289. * @return \Illuminate\Database\Eloquent\Collection
  290. */
  291. public function findMany($ids, $columns = ['*'])
  292. {
  293. $ids = $ids instanceof Arrayable ? $ids->toArray() : $ids;
  294. if (empty($ids)) {
  295. return $this->model->newCollection();
  296. }
  297. return $this->whereKey($ids)->get($columns);
  298. }
  299. /**
  300. * Find a model by its primary key or throw an exception.
  301. *
  302. * @param mixed $id
  303. * @param array $columns
  304. * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static|static[]
  305. *
  306. * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  307. */
  308. public function findOrFail($id, $columns = ['*'])
  309. {
  310. $result = $this->find($id, $columns);
  311. if (is_array($id)) {
  312. if (count($result) === count(array_unique($id))) {
  313. return $result;
  314. }
  315. } elseif (! is_null($result)) {
  316. return $result;
  317. }
  318. throw (new ModelNotFoundException)->setModel(
  319. get_class($this->model), $id
  320. );
  321. }
  322. /**
  323. * Find a model by its primary key or return fresh model instance.
  324. *
  325. * @param mixed $id
  326. * @param array $columns
  327. * @return \Illuminate\Database\Eloquent\Model|static
  328. */
  329. public function findOrNew($id, $columns = ['*'])
  330. {
  331. if (! is_null($model = $this->find($id, $columns))) {
  332. return $model;
  333. }
  334. return $this->newModelInstance();
  335. }
  336. /**
  337. * Get the first record matching the attributes or instantiate it.
  338. *
  339. * @param array $attributes
  340. * @param array $values
  341. * @return \Illuminate\Database\Eloquent\Model|static
  342. */
  343. public function firstOrNew(array $attributes, array $values = [])
  344. {
  345. if (! is_null($instance = $this->where($attributes)->first())) {
  346. return $instance;
  347. }
  348. return $this->newModelInstance($attributes + $values);
  349. }
  350. /**
  351. * Get the first record matching the attributes or create it.
  352. *
  353. * @param array $attributes
  354. * @param array $values
  355. * @return \Illuminate\Database\Eloquent\Model|static
  356. */
  357. public function firstOrCreate(array $attributes, array $values = [])
  358. {
  359. if (! is_null($instance = $this->where($attributes)->first())) {
  360. return $instance;
  361. }
  362. return tap($this->newModelInstance($attributes + $values), function ($instance) {
  363. $instance->save();
  364. });
  365. }
  366. /**
  367. * Create or update a record matching the attributes, and fill it with values.
  368. *
  369. * @param array $attributes
  370. * @param array $values
  371. * @return \Illuminate\Database\Eloquent\Model|static
  372. */
  373. public function updateOrCreate(array $attributes, array $values = [])
  374. {
  375. return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
  376. $instance->fill($values)->save();
  377. });
  378. }
  379. /**
  380. * Execute the query and get the first result or throw an exception.
  381. *
  382. * @param array $columns
  383. * @return \Illuminate\Database\Eloquent\Model|static
  384. *
  385. * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  386. */
  387. public function firstOrFail($columns = ['*'])
  388. {
  389. if (! is_null($model = $this->first($columns))) {
  390. return $model;
  391. }
  392. throw (new ModelNotFoundException)->setModel(get_class($this->model));
  393. }
  394. /**
  395. * Execute the query and get the first result or call a callback.
  396. *
  397. * @param \Closure|array $columns
  398. * @param \Closure|null $callback
  399. * @return \Illuminate\Database\Eloquent\Model|static|mixed
  400. */
  401. public function firstOr($columns = ['*'], Closure $callback = null)
  402. {
  403. if ($columns instanceof Closure) {
  404. $callback = $columns;
  405. $columns = ['*'];
  406. }
  407. if (! is_null($model = $this->first($columns))) {
  408. return $model;
  409. }
  410. return call_user_func($callback);
  411. }
  412. /**
  413. * Get a single column's value from the first result of a query.
  414. *
  415. * @param string $column
  416. * @return mixed
  417. */
  418. public function value($column)
  419. {
  420. if ($result = $this->first([$column])) {
  421. return $result->{$column};
  422. }
  423. }
  424. /**
  425. * Execute the query as a "select" statement.
  426. *
  427. * @param array $columns
  428. * @return \Illuminate\Database\Eloquent\Collection|static[]
  429. */
  430. public function get($columns = ['*'])
  431. {
  432. $builder = $this->applyScopes();
  433. // If we actually found models we will also eager load any relationships that
  434. // have been specified as needing to be eager loaded, which will solve the
  435. // n+1 query issue for the developers to avoid running a lot of queries.
  436. if (count($models = $builder->getModels($columns)) > 0) {
  437. $models = $builder->eagerLoadRelations($models);
  438. }
  439. return $builder->getModel()->newCollection($models);
  440. }
  441. /**
  442. * Get the hydrated models without eager loading.
  443. *
  444. * @param array $columns
  445. * @return \Illuminate\Database\Eloquent\Model[]|static[]
  446. */
  447. public function getModels($columns = ['*'])
  448. {
  449. return $this->model->hydrate(
  450. $this->query->get($columns)->all()
  451. )->all();
  452. }
  453. /**
  454. * Eager load the relationships for the models.
  455. *
  456. * @param array $models
  457. * @return array
  458. */
  459. public function eagerLoadRelations(array $models)
  460. {
  461. foreach ($this->eagerLoad as $name => $constraints) {
  462. // For nested eager loads we'll skip loading them here and they will be set as an
  463. // eager load on the query to retrieve the relation so that they will be eager
  464. // loaded on that query, because that is where they get hydrated as models.
  465. if (strpos($name, '.') === false) {
  466. $models = $this->eagerLoadRelation($models, $name, $constraints);
  467. }
  468. }
  469. return $models;
  470. }
  471. /**
  472. * Eagerly load the relationship on a set of models.
  473. *
  474. * @param array $models
  475. * @param string $name
  476. * @param \Closure $constraints
  477. * @return array
  478. */
  479. protected function eagerLoadRelation(array $models, $name, Closure $constraints)
  480. {
  481. // First we will "back up" the existing where conditions on the query so we can
  482. // add our eager constraints. Then we will merge the wheres that were on the
  483. // query back to it in order that any where conditions might be specified.
  484. $relation = $this->getRelation($name);
  485. $relation->addEagerConstraints($models);
  486. $constraints($relation);
  487. // Once we have the results, we just match those back up to their parent models
  488. // using the relationship instance. Then we just return the finished arrays
  489. // of models which have been eagerly hydrated and are readied for return.
  490. return $relation->match(
  491. $relation->initRelation($models, $name),
  492. $relation->getEager(), $name
  493. );
  494. }
  495. /**
  496. * Get the relation instance for the given relation name.
  497. *
  498. * @param string $name
  499. * @return \Illuminate\Database\Eloquent\Relations\Relation
  500. */
  501. public function getRelation($name)
  502. {
  503. // We want to run a relationship query without any constrains so that we will
  504. // not have to remove these where clauses manually which gets really hacky
  505. // and error prone. We don't want constraints because we add eager ones.
  506. $relation = Relation::noConstraints(function () use ($name) {
  507. try {
  508. return $this->getModel()->newInstance()->$name();
  509. } catch (BadMethodCallException $e) {
  510. throw RelationNotFoundException::make($this->getModel(), $name);
  511. }
  512. });
  513. $nested = $this->relationsNestedUnder($name);
  514. // If there are nested relationships set on the query, we will put those onto
  515. // the query instances so that they can be handled after this relationship
  516. // is loaded. In this way they will all trickle down as they are loaded.
  517. if (count($nested) > 0) {
  518. $relation->getQuery()->with($nested);
  519. }
  520. return $relation;
  521. }
  522. /**
  523. * Get the deeply nested relations for a given top-level relation.
  524. *
  525. * @param string $relation
  526. * @return array
  527. */
  528. protected function relationsNestedUnder($relation)
  529. {
  530. $nested = [];
  531. // We are basically looking for any relationships that are nested deeper than
  532. // the given top-level relationship. We will just check for any relations
  533. // that start with the given top relations and adds them to our arrays.
  534. foreach ($this->eagerLoad as $name => $constraints) {
  535. if ($this->isNestedUnder($relation, $name)) {
  536. $nested[substr($name, strlen($relation.'.'))] = $constraints;
  537. }
  538. }
  539. return $nested;
  540. }
  541. /**
  542. * Determine if the relationship is nested.
  543. *
  544. * @param string $relation
  545. * @param string $name
  546. * @return bool
  547. */
  548. protected function isNestedUnder($relation, $name)
  549. {
  550. return Str::contains($name, '.') && Str::startsWith($name, $relation.'.');
  551. }
  552. /**
  553. * Get a generator for the given query.
  554. *
  555. * @return \Generator
  556. */
  557. public function cursor()
  558. {
  559. foreach ($this->applyScopes()->query->cursor() as $record) {
  560. yield $this->newModelInstance()->newFromBuilder($record);
  561. }
  562. }
  563. /**
  564. * Chunk the results of a query by comparing numeric IDs.
  565. *
  566. * @param int $count
  567. * @param callable $callback
  568. * @param string|null $column
  569. * @param string|null $alias
  570. * @return bool
  571. */
  572. public function chunkById($count, callable $callback, $column = null, $alias = null)
  573. {
  574. $column = is_null($column) ? $this->getModel()->getKeyName() : $column;
  575. $alias = is_null($alias) ? $column : $alias;
  576. $lastId = null;
  577. do {
  578. $clone = clone $this;
  579. // We'll execute the query for the given page and get the results. If there are
  580. // no results we can just break and return from here. When there are results
  581. // we will call the callback with the current chunk of these results here.
  582. $results = $clone->forPageAfterId($count, $lastId, $column)->get();
  583. $countResults = $results->count();
  584. if ($countResults == 0) {
  585. break;
  586. }
  587. // On each chunk result set, we will pass them to the callback and then let the
  588. // developer take care of everything within the callback, which allows us to
  589. // keep the memory low for spinning through large result sets for working.
  590. if ($callback($results) === false) {
  591. return false;
  592. }
  593. $lastId = $results->last()->{$alias};
  594. unset($results);
  595. } while ($countResults == $count);
  596. return true;
  597. }
  598. /**
  599. * Add a generic "order by" clause if the query doesn't already have one.
  600. *
  601. * @return void
  602. */
  603. protected function enforceOrderBy()
  604. {
  605. if (empty($this->query->orders) && empty($this->query->unionOrders)) {
  606. $this->orderBy($this->model->getQualifiedKeyName(), 'asc');
  607. }
  608. }
  609. /**
  610. * Get an array with the values of a given column.
  611. *
  612. * @param string $column
  613. * @param string|null $key
  614. * @return \Illuminate\Support\Collection
  615. */
  616. public function pluck($column, $key = null)
  617. {
  618. $results = $this->toBase()->pluck($column, $key);
  619. // If the model has a mutator for the requested column, we will spin through
  620. // the results and mutate the values so that the mutated version of these
  621. // columns are returned as you would expect from these Eloquent models.
  622. if (! $this->model->hasGetMutator($column) &&
  623. ! $this->model->hasCast($column) &&
  624. ! in_array($column, $this->model->getDates())) {
  625. return $results;
  626. }
  627. return $results->map(function ($value) use ($column) {
  628. return $this->model->newFromBuilder([$column => $value])->{$column};
  629. });
  630. }
  631. /**
  632. * Paginate the given query.
  633. *
  634. * @param int $perPage
  635. * @param array $columns
  636. * @param string $pageName
  637. * @param int|null $page
  638. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  639. *
  640. * @throws \InvalidArgumentException
  641. */
  642. public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
  643. {
  644. $page = $page ?: Paginator::resolveCurrentPage($pageName);
  645. $perPage = $perPage ?: $this->model->getPerPage();
  646. $results = ($total = $this->toBase()->getCountForPagination())
  647. ? $this->forPage($page, $perPage)->get($columns)
  648. : $this->model->newCollection();
  649. return $this->paginator($results, $total, $perPage, $page, [
  650. 'path' => Paginator::resolveCurrentPath(),
  651. 'pageName' => $pageName,
  652. ]);
  653. }
  654. /**
  655. * Paginate the given query into a simple paginator.
  656. *
  657. * @param int $perPage
  658. * @param array $columns
  659. * @param string $pageName
  660. * @param int|null $page
  661. * @return \Illuminate\Contracts\Pagination\Paginator
  662. */
  663. public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
  664. {
  665. $page = $page ?: Paginator::resolveCurrentPage($pageName);
  666. $perPage = $perPage ?: $this->model->getPerPage();
  667. // Next we will set the limit and offset for this query so that when we get the
  668. // results we get the proper section of results. Then, we'll create the full
  669. // paginator instances for these results with the given page and per page.
  670. $this->skip(($page - 1) * $perPage)->take($perPage + 1);
  671. return $this->simplePaginator($this->get($columns), $perPage, $page, [
  672. 'path' => Paginator::resolveCurrentPath(),
  673. 'pageName' => $pageName,
  674. ]);
  675. }
  676. /**
  677. * Save a new model and return the instance.
  678. *
  679. * @param array $attributes
  680. * @return \Illuminate\Database\Eloquent\Model|$this
  681. */
  682. public function create(array $attributes = [])
  683. {
  684. return tap($this->newModelInstance($attributes), function ($instance) {
  685. $instance->save();
  686. });
  687. }
  688. /**
  689. * Save a new model and return the instance. Allow mass-assignment.
  690. *
  691. * @param array $attributes
  692. * @return \Illuminate\Database\Eloquent\Model|$this
  693. */
  694. public function forceCreate(array $attributes)
  695. {
  696. return $this->model->unguarded(function () use ($attributes) {
  697. return $this->newModelInstance()->create($attributes);
  698. });
  699. }
  700. /**
  701. * Update a record in the database.
  702. *
  703. * @param array $values
  704. * @return int
  705. */
  706. public function update(array $values)
  707. {
  708. return $this->toBase()->update($this->addUpdatedAtColumn($values));
  709. }
  710. /**
  711. * Increment a column's value by a given amount.
  712. *
  713. * @param string $column
  714. * @param float|int $amount
  715. * @param array $extra
  716. * @return int
  717. */
  718. public function increment($column, $amount = 1, array $extra = [])
  719. {
  720. return $this->toBase()->increment(
  721. $column, $amount, $this->addUpdatedAtColumn($extra)
  722. );
  723. }
  724. /**
  725. * Decrement a column's value by a given amount.
  726. *
  727. * @param string $column
  728. * @param float|int $amount
  729. * @param array $extra
  730. * @return int
  731. */
  732. public function decrement($column, $amount = 1, array $extra = [])
  733. {
  734. return $this->toBase()->decrement(
  735. $column, $amount, $this->addUpdatedAtColumn($extra)
  736. );
  737. }
  738. /**
  739. * Add the "updated at" column to an array of values.
  740. *
  741. * @param array $values
  742. * @return array
  743. */
  744. protected function addUpdatedAtColumn(array $values)
  745. {
  746. if (! $this->model->usesTimestamps() ||
  747. is_null($this->model->getUpdatedAtColumn())) {
  748. return $values;
  749. }
  750. $column = $this->model->getUpdatedAtColumn();
  751. $values = array_merge(
  752. [$column => $this->model->freshTimestampString()],
  753. $values
  754. );
  755. $segments = preg_split('/\s+as\s+/i', $this->query->from);
  756. $qualifiedColumn = end($segments).'.'.$column;
  757. $values[$qualifiedColumn] = $values[$column];
  758. unset($values[$column]);
  759. return $values;
  760. }
  761. /**
  762. * Delete a record from the database.
  763. *
  764. * @return mixed
  765. */
  766. public function delete()
  767. {
  768. if (isset($this->onDelete)) {
  769. return call_user_func($this->onDelete, $this);
  770. }
  771. return $this->toBase()->delete();
  772. }
  773. /**
  774. * Run the default delete function on the builder.
  775. *
  776. * Since we do not apply scopes here, the row will actually be deleted.
  777. *
  778. * @return mixed
  779. */
  780. public function forceDelete()
  781. {
  782. return $this->query->delete();
  783. }
  784. /**
  785. * Register a replacement for the default delete function.
  786. *
  787. * @param \Closure $callback
  788. * @return void
  789. */
  790. public function onDelete(Closure $callback)
  791. {
  792. $this->onDelete = $callback;
  793. }
  794. /**
  795. * Call the given local model scopes.
  796. *
  797. * @param array $scopes
  798. * @return static|mixed
  799. */
  800. public function scopes(array $scopes)
  801. {
  802. $builder = $this;
  803. foreach ($scopes as $scope => $parameters) {
  804. // If the scope key is an integer, then the scope was passed as the value and
  805. // the parameter list is empty, so we will format the scope name and these
  806. // parameters here. Then, we'll be ready to call the scope on the model.
  807. if (is_int($scope)) {
  808. [$scope, $parameters] = [$parameters, []];
  809. }
  810. // Next we'll pass the scope callback to the callScope method which will take
  811. // care of grouping the "wheres" properly so the logical order doesn't get
  812. // messed up when adding scopes. Then we'll return back out the builder.
  813. $builder = $builder->callScope(
  814. [$this->model, 'scope'.ucfirst($scope)],
  815. (array) $parameters
  816. );
  817. }
  818. return $builder;
  819. }
  820. /**
  821. * Apply the scopes to the Eloquent builder instance and return it.
  822. *
  823. * @return static
  824. */
  825. public function applyScopes()
  826. {
  827. if (! $this->scopes) {
  828. return $this;
  829. }
  830. $builder = clone $this;
  831. foreach ($this->scopes as $identifier => $scope) {
  832. if (! isset($builder->scopes[$identifier])) {
  833. continue;
  834. }
  835. $builder->callScope(function (self $builder) use ($scope) {
  836. // If the scope is a Closure we will just go ahead and call the scope with the
  837. // builder instance. The "callScope" method will properly group the clauses
  838. // that are added to this query so "where" clauses maintain proper logic.
  839. if ($scope instanceof Closure) {
  840. $scope($builder);
  841. }
  842. // If the scope is a scope object, we will call the apply method on this scope
  843. // passing in the builder and the model instance. After we run all of these
  844. // scopes we will return back the builder instance to the outside caller.
  845. if ($scope instanceof Scope) {
  846. $scope->apply($builder, $this->getModel());
  847. }
  848. });
  849. }
  850. return $builder;
  851. }
  852. /**
  853. * Apply the given scope on the current builder instance.
  854. *
  855. * @param callable $scope
  856. * @param array $parameters
  857. * @return mixed
  858. */
  859. protected function callScope(callable $scope, $parameters = [])
  860. {
  861. array_unshift($parameters, $this);
  862. $query = $this->getQuery();
  863. // We will keep track of how many wheres are on the query before running the
  864. // scope so that we can properly group the added scope constraints in the
  865. // query as their own isolated nested where statement and avoid issues.
  866. $originalWhereCount = is_null($query->wheres)
  867. ? 0 : count($query->wheres);
  868. $result = $scope(...array_values($parameters)) ?? $this;
  869. if (count((array) $query->wheres) > $originalWhereCount) {
  870. $this->addNewWheresWithinGroup($query, $originalWhereCount);
  871. }
  872. return $result;
  873. }
  874. /**
  875. * Nest where conditions by slicing them at the given where count.
  876. *
  877. * @param \Illuminate\Database\Query\Builder $query
  878. * @param int $originalWhereCount
  879. * @return void
  880. */
  881. protected function addNewWheresWithinGroup(QueryBuilder $query, $originalWhereCount)
  882. {
  883. // Here, we totally remove all of the where clauses since we are going to
  884. // rebuild them as nested queries by slicing the groups of wheres into
  885. // their own sections. This is to prevent any confusing logic order.
  886. $allWheres = $query->wheres;
  887. $query->wheres = [];
  888. $this->groupWhereSliceForScope(
  889. $query, array_slice($allWheres, 0, $originalWhereCount)
  890. );
  891. $this->groupWhereSliceForScope(
  892. $query, array_slice($allWheres, $originalWhereCount)
  893. );
  894. }
  895. /**
  896. * Slice where conditions at the given offset and add them to the query as a nested condition.
  897. *
  898. * @param \Illuminate\Database\Query\Builder $query
  899. * @param array $whereSlice
  900. * @return void
  901. */
  902. protected function groupWhereSliceForScope(QueryBuilder $query, $whereSlice)
  903. {
  904. $whereBooleans = collect($whereSlice)->pluck('boolean');
  905. // Here we'll check if the given subset of where clauses contains any "or"
  906. // booleans and in this case create a nested where expression. That way
  907. // we don't add any unnecessary nesting thus keeping the query clean.
  908. if ($whereBooleans->contains('or')) {
  909. $query->wheres[] = $this->createNestedWhere(
  910. $whereSlice, $whereBooleans->first()
  911. );
  912. } else {
  913. $query->wheres = array_merge($query->wheres, $whereSlice);
  914. }
  915. }
  916. /**
  917. * Create a where array with nested where conditions.
  918. *
  919. * @param array $whereSlice
  920. * @param string $boolean
  921. * @return array
  922. */
  923. protected function createNestedWhere($whereSlice, $boolean = 'and')
  924. {
  925. $whereGroup = $this->getQuery()->forNestedWhere();
  926. $whereGroup->wheres = $whereSlice;
  927. return ['type' => 'Nested', 'query' => $whereGroup, 'boolean' => $boolean];
  928. }
  929. /**
  930. * Set the relationships that should be eager loaded.
  931. *
  932. * @param mixed $relations
  933. * @return $this
  934. */
  935. public function with($relations)
  936. {
  937. $eagerLoad = $this->parseWithRelations(is_string($relations) ? func_get_args() : $relations);
  938. $this->eagerLoad = array_merge($this->eagerLoad, $eagerLoad);
  939. return $this;
  940. }
  941. /**
  942. * Prevent the specified relations from being eager loaded.
  943. *
  944. * @param mixed $relations
  945. * @return $this
  946. */
  947. public function without($relations)
  948. {
  949. $this->eagerLoad = array_diff_key($this->eagerLoad, array_flip(
  950. is_string($relations) ? func_get_args() : $relations
  951. ));
  952. return $this;
  953. }
  954. /**
  955. * Create a new instance of the model being queried.
  956. *
  957. * @param array $attributes
  958. * @return \Illuminate\Database\Eloquent\Model|static
  959. */
  960. public function newModelInstance($attributes = [])
  961. {
  962. return $this->model->newInstance($attributes)->setConnection(
  963. $this->query->getConnection()->getName()
  964. );
  965. }
  966. /**
  967. * Parse a list of relations into individuals.
  968. *
  969. * @param array $relations
  970. * @return array
  971. */
  972. protected function parseWithRelations(array $relations)
  973. {
  974. $results = [];
  975. foreach ($relations as $name => $constraints) {
  976. // If the "name" value is a numeric key, we can assume that no
  977. // constraints have been specified. We'll just put an empty
  978. // Closure there, so that we can treat them all the same.
  979. if (is_numeric($name)) {
  980. $name = $constraints;
  981. [$name, $constraints] = Str::contains($name, ':')
  982. ? $this->createSelectWithConstraint($name)
  983. : [$name, function () {
  984. //
  985. }];
  986. }
  987. // We need to separate out any nested includes, which allows the developers
  988. // to load deep relationships using "dots" without stating each level of
  989. // the relationship with its own key in the array of eager-load names.
  990. $results = $this->addNestedWiths($name, $results);
  991. $results[$name] = $constraints;
  992. }
  993. return $results;
  994. }
  995. /**
  996. * Create a constraint to select the given columns for the relation.
  997. *
  998. * @param string $name
  999. * @return array
  1000. */
  1001. protected function createSelectWithConstraint($name)
  1002. {
  1003. return [explode(':', $name)[0], function ($query) use ($name) {
  1004. $query->select(explode(',', explode(':', $name)[1]));
  1005. }];
  1006. }
  1007. /**
  1008. * Parse the nested relationships in a relation.
  1009. *
  1010. * @param string $name
  1011. * @param array $results
  1012. * @return array
  1013. */
  1014. protected function addNestedWiths($name, $results)
  1015. {
  1016. $progress = [];
  1017. // If the relation has already been set on the result array, we will not set it
  1018. // again, since that would override any constraints that were already placed
  1019. // on the relationships. We will only set the ones that are not specified.
  1020. foreach (explode('.', $name) as $segment) {
  1021. $progress[] = $segment;
  1022. if (! isset($results[$last = implode('.', $progress)])) {
  1023. $results[$last] = function () {
  1024. //
  1025. };
  1026. }
  1027. }
  1028. return $results;
  1029. }
  1030. /**
  1031. * Get the underlying query builder instance.
  1032. *
  1033. * @return \Illuminate\Database\Query\Builder
  1034. */
  1035. public function getQuery()
  1036. {
  1037. return $this->query;
  1038. }
  1039. /**
  1040. * Set the underlying query builder instance.
  1041. *
  1042. * @param \Illuminate\Database\Query\Builder $query
  1043. * @return $this
  1044. */
  1045. public function setQuery($query)
  1046. {
  1047. $this->query = $query;
  1048. return $this;
  1049. }
  1050. /**
  1051. * Get a base query builder instance.
  1052. *
  1053. * @return \Illuminate\Database\Query\Builder
  1054. */
  1055. public function toBase()
  1056. {
  1057. return $this->applyScopes()->getQuery();
  1058. }
  1059. /**
  1060. * Get the relationships being eagerly loaded.
  1061. *
  1062. * @return array
  1063. */
  1064. public function getEagerLoads()
  1065. {
  1066. return $this->eagerLoad;
  1067. }
  1068. /**
  1069. * Set the relationships being eagerly loaded.
  1070. *
  1071. * @param array $eagerLoad
  1072. * @return $this
  1073. */
  1074. public function setEagerLoads(array $eagerLoad)
  1075. {
  1076. $this->eagerLoad = $eagerLoad;
  1077. return $this;
  1078. }
  1079. /**
  1080. * Get the model instance being queried.
  1081. *
  1082. * @return \Illuminate\Database\Eloquent\Model|static
  1083. */
  1084. public function getModel()
  1085. {
  1086. return $this->model;
  1087. }
  1088. /**
  1089. * Set a model instance for the model being queried.
  1090. *
  1091. * @param \Illuminate\Database\Eloquent\Model $model
  1092. * @return $this
  1093. */
  1094. public function setModel(Model $model)
  1095. {
  1096. $this->model = $model;
  1097. $this->query->from($model->getTable());
  1098. return $this;
  1099. }
  1100. /**
  1101. * Qualify the given column name by the model's table.
  1102. *
  1103. * @param string $column
  1104. * @return string
  1105. */
  1106. public function qualifyColumn($column)
  1107. {
  1108. return $this->model->qualifyColumn($column);
  1109. }
  1110. /**
  1111. * Get the given macro by name.
  1112. *
  1113. * @param string $name
  1114. * @return \Closure
  1115. */
  1116. public function getMacro($name)
  1117. {
  1118. return Arr::get($this->localMacros, $name);
  1119. }
  1120. /**
  1121. * Dynamically access builder proxies.
  1122. *
  1123. * @param string $key
  1124. * @return mixed
  1125. *
  1126. * @throws \Exception
  1127. */
  1128. public function __get($key)
  1129. {
  1130. if ($key === 'orWhere') {
  1131. return new HigherOrderBuilderProxy($this, $key);
  1132. }
  1133. throw new Exception("Property [{$key}] does not exist on the Eloquent builder instance.");
  1134. }
  1135. /**
  1136. * Dynamically handle calls into the query instance.
  1137. *
  1138. * @param string $method
  1139. * @param array $parameters
  1140. * @return mixed
  1141. */
  1142. public function __call($method, $parameters)
  1143. {
  1144. if ($method === 'macro') {
  1145. $this->localMacros[$parameters[0]] = $parameters[1];
  1146. return;
  1147. }
  1148. if (isset($this->localMacros[$method])) {
  1149. array_unshift($parameters, $this);
  1150. return $this->localMacros[$method](...$parameters);
  1151. }
  1152. if (isset(static::$macros[$method])) {
  1153. if (static::$macros[$method] instanceof Closure) {
  1154. return call_user_func_array(static::$macros[$method]->bindTo($this, static::class), $parameters);
  1155. }
  1156. return call_user_func_array(static::$macros[$method], $parameters);
  1157. }
  1158. if (method_exists($this->model, $scope = 'scope'.ucfirst($method))) {
  1159. return $this->callScope([$this->model, $scope], $parameters);
  1160. }
  1161. if (in_array($method, $this->passthru)) {
  1162. return $this->toBase()->{$method}(...$parameters);
  1163. }
  1164. $this->forwardCallTo($this->query, $method, $parameters);
  1165. return $this;
  1166. }
  1167. /**
  1168. * Dynamically handle calls into the query instance.
  1169. *
  1170. * @param string $method
  1171. * @param array $parameters
  1172. * @return mixed
  1173. *
  1174. * @throws \BadMethodCallException
  1175. */
  1176. public static function __callStatic($method, $parameters)
  1177. {
  1178. if ($method === 'macro') {
  1179. static::$macros[$parameters[0]] = $parameters[1];
  1180. return;
  1181. }
  1182. if (! isset(static::$macros[$method])) {
  1183. static::throwBadMethodCallException($method);
  1184. }
  1185. if (static::$macros[$method] instanceof Closure) {
  1186. return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters);
  1187. }
  1188. return call_user_func_array(static::$macros[$method], $parameters);
  1189. }
  1190. /**
  1191. * Force a clone of the underlying query builder when cloning.
  1192. *
  1193. * @return void
  1194. */
  1195. public function __clone()
  1196. {
  1197. $this->query = clone $this->query;
  1198. }
  1199. }