Builder.php 37KB

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