PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php

https://gitlab.com/ealexis.t/trends
PHP | 1832 lines | 696 code | 259 blank | 877 comment | 48 complexity | 288324a0a85379fc4637055e29ca0af6 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database\Query;
  3. use Closure;
  4. use RuntimeException;
  5. use BadMethodCallException;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Str;
  8. use InvalidArgumentException;
  9. use Illuminate\Pagination\Paginator;
  10. use Illuminate\Support\Traits\Macroable;
  11. use Illuminate\Contracts\Support\Arrayable;
  12. use Illuminate\Database\ConnectionInterface;
  13. use Illuminate\Database\Query\Grammars\Grammar;
  14. use Illuminate\Pagination\LengthAwarePaginator;
  15. use Illuminate\Database\Query\Processors\Processor;
  16. class Builder
  17. {
  18. use Macroable {
  19. __call as macroCall;
  20. }
  21. /**
  22. * The database connection instance.
  23. *
  24. * @var \Illuminate\Database\Connection
  25. */
  26. protected $connection;
  27. /**
  28. * The database query grammar instance.
  29. *
  30. * @var \Illuminate\Database\Query\Grammars\Grammar
  31. */
  32. protected $grammar;
  33. /**
  34. * The database query post processor instance.
  35. *
  36. * @var \Illuminate\Database\Query\Processors\Processor
  37. */
  38. protected $processor;
  39. /**
  40. * The current query value bindings.
  41. *
  42. * @var array
  43. */
  44. protected $bindings = [
  45. 'select' => [],
  46. 'join' => [],
  47. 'where' => [],
  48. 'having' => [],
  49. 'order' => [],
  50. 'union' => [],
  51. ];
  52. /**
  53. * An aggregate function and column to be run.
  54. *
  55. * @var array
  56. */
  57. public $aggregate;
  58. /**
  59. * The columns that should be returned.
  60. *
  61. * @var array
  62. */
  63. public $columns;
  64. /**
  65. * Indicates if the query returns distinct results.
  66. *
  67. * @var bool
  68. */
  69. public $distinct = false;
  70. /**
  71. * The table which the query is targeting.
  72. *
  73. * @var string
  74. */
  75. public $from;
  76. /**
  77. * The table joins for the query.
  78. *
  79. * @var array
  80. */
  81. public $joins;
  82. /**
  83. * The where constraints for the query.
  84. *
  85. * @var array
  86. */
  87. public $wheres;
  88. /**
  89. * The groupings for the query.
  90. *
  91. * @var array
  92. */
  93. public $groups;
  94. /**
  95. * The having constraints for the query.
  96. *
  97. * @var array
  98. */
  99. public $havings;
  100. /**
  101. * The orderings for the query.
  102. *
  103. * @var array
  104. */
  105. public $orders;
  106. /**
  107. * The maximum number of records to return.
  108. *
  109. * @var int
  110. */
  111. public $limit;
  112. /**
  113. * The number of records to skip.
  114. *
  115. * @var int
  116. */
  117. public $offset;
  118. /**
  119. * The query union statements.
  120. *
  121. * @var array
  122. */
  123. public $unions;
  124. /**
  125. * The maximum number of union records to return.
  126. *
  127. * @var int
  128. */
  129. public $unionLimit;
  130. /**
  131. * The number of union records to skip.
  132. *
  133. * @var int
  134. */
  135. public $unionOffset;
  136. /**
  137. * The orderings for the union query.
  138. *
  139. * @var array
  140. */
  141. public $unionOrders;
  142. /**
  143. * Indicates whether row locking is being used.
  144. *
  145. * @var string|bool
  146. */
  147. public $lock;
  148. /**
  149. * The field backups currently in use.
  150. *
  151. * @var array
  152. */
  153. protected $backups = [];
  154. /**
  155. * The binding backups currently in use.
  156. *
  157. * @var array
  158. */
  159. protected $bindingBackups = [];
  160. /**
  161. * All of the available clause operators.
  162. *
  163. * @var array
  164. */
  165. protected $operators = [
  166. '=', '<', '>', '<=', '>=', '<>', '!=',
  167. 'like', 'like binary', 'not like', 'between', 'ilike',
  168. '&', '|', '^', '<<', '>>',
  169. 'rlike', 'regexp', 'not regexp',
  170. '~', '~*', '!~', '!~*', 'similar to',
  171. 'not similar to',
  172. ];
  173. /**
  174. * Whether use write pdo for select.
  175. *
  176. * @var bool
  177. */
  178. protected $useWritePdo = false;
  179. /**
  180. * Create a new query builder instance.
  181. *
  182. * @param \Illuminate\Database\ConnectionInterface $connection
  183. * @param \Illuminate\Database\Query\Grammars\Grammar $grammar
  184. * @param \Illuminate\Database\Query\Processors\Processor $processor
  185. * @return void
  186. */
  187. public function __construct(ConnectionInterface $connection,
  188. Grammar $grammar = null,
  189. Processor $processor = null)
  190. {
  191. $this->connection = $connection;
  192. $this->grammar = $grammar ?: $connection->getQueryGrammar();
  193. $this->processor = $processor ?: $connection->getPostProcessor();
  194. }
  195. /**
  196. * Set the columns to be selected.
  197. *
  198. * @param array|mixed $columns
  199. * @return $this
  200. */
  201. public function select($columns = ['*'])
  202. {
  203. $this->columns = is_array($columns) ? $columns : func_get_args();
  204. return $this;
  205. }
  206. /**
  207. * Add a new "raw" select expression to the query.
  208. *
  209. * @param string $expression
  210. * @param array $bindings
  211. * @return \Illuminate\Database\Query\Builder|static
  212. */
  213. public function selectRaw($expression, array $bindings = [])
  214. {
  215. $this->addSelect(new Expression($expression));
  216. if ($bindings) {
  217. $this->addBinding($bindings, 'select');
  218. }
  219. return $this;
  220. }
  221. /**
  222. * Add a subselect expression to the query.
  223. *
  224. * @param \Closure|\Illuminate\Database\Query\Builder|string $query
  225. * @param string $as
  226. * @return \Illuminate\Database\Query\Builder|static
  227. *
  228. * @throws \InvalidArgumentException
  229. */
  230. public function selectSub($query, $as)
  231. {
  232. if ($query instanceof Closure) {
  233. $callback = $query;
  234. $callback($query = $this->newQuery());
  235. }
  236. if ($query instanceof self) {
  237. $bindings = $query->getBindings();
  238. $query = $query->toSql();
  239. } elseif (is_string($query)) {
  240. $bindings = [];
  241. } else {
  242. throw new InvalidArgumentException;
  243. }
  244. return $this->selectRaw('('.$query.') as '.$this->grammar->wrap($as), $bindings);
  245. }
  246. /**
  247. * Add a new select column to the query.
  248. *
  249. * @param array|mixed $column
  250. * @return $this
  251. */
  252. public function addSelect($column)
  253. {
  254. $column = is_array($column) ? $column : func_get_args();
  255. $this->columns = array_merge((array) $this->columns, $column);
  256. return $this;
  257. }
  258. /**
  259. * Force the query to only return distinct results.
  260. *
  261. * @return $this
  262. */
  263. public function distinct()
  264. {
  265. $this->distinct = true;
  266. return $this;
  267. }
  268. /**
  269. * Set the table which the query is targeting.
  270. *
  271. * @param string $table
  272. * @return $this
  273. */
  274. public function from($table)
  275. {
  276. $this->from = $table;
  277. return $this;
  278. }
  279. /**
  280. * Add a join clause to the query.
  281. *
  282. * @param string $table
  283. * @param string $one
  284. * @param string $operator
  285. * @param string $two
  286. * @param string $type
  287. * @param bool $where
  288. * @return $this
  289. */
  290. public function join($table, $one, $operator = null, $two = null, $type = 'inner', $where = false)
  291. {
  292. // If the first "column" of the join is really a Closure instance the developer
  293. // is trying to build a join with a complex "on" clause containing more than
  294. // one condition, so we'll add the join and call a Closure with the query.
  295. if ($one instanceof Closure) {
  296. $join = new JoinClause($type, $table);
  297. call_user_func($one, $join);
  298. $this->joins[] = $join;
  299. $this->addBinding($join->bindings, 'join');
  300. }
  301. // If the column is simply a string, we can assume the join simply has a basic
  302. // "on" clause with a single condition. So we will just build the join with
  303. // this simple join clauses attached to it. There is not a join callback.
  304. else {
  305. $join = new JoinClause($type, $table);
  306. $this->joins[] = $join->on(
  307. $one, $operator, $two, 'and', $where
  308. );
  309. $this->addBinding($join->bindings, 'join');
  310. }
  311. return $this;
  312. }
  313. /**
  314. * Add a "join where" clause to the query.
  315. *
  316. * @param string $table
  317. * @param string $one
  318. * @param string $operator
  319. * @param string $two
  320. * @param string $type
  321. * @return \Illuminate\Database\Query\Builder|static
  322. */
  323. public function joinWhere($table, $one, $operator, $two, $type = 'inner')
  324. {
  325. return $this->join($table, $one, $operator, $two, $type, true);
  326. }
  327. /**
  328. * Add a left join to the query.
  329. *
  330. * @param string $table
  331. * @param string $first
  332. * @param string $operator
  333. * @param string $second
  334. * @return \Illuminate\Database\Query\Builder|static
  335. */
  336. public function leftJoin($table, $first, $operator = null, $second = null)
  337. {
  338. return $this->join($table, $first, $operator, $second, 'left');
  339. }
  340. /**
  341. * Add a "join where" clause to the query.
  342. *
  343. * @param string $table
  344. * @param string $one
  345. * @param string $operator
  346. * @param string $two
  347. * @return \Illuminate\Database\Query\Builder|static
  348. */
  349. public function leftJoinWhere($table, $one, $operator, $two)
  350. {
  351. return $this->joinWhere($table, $one, $operator, $two, 'left');
  352. }
  353. /**
  354. * Add a right join to the query.
  355. *
  356. * @param string $table
  357. * @param string $first
  358. * @param string $operator
  359. * @param string $second
  360. * @return \Illuminate\Database\Query\Builder|static
  361. */
  362. public function rightJoin($table, $first, $operator = null, $second = null)
  363. {
  364. return $this->join($table, $first, $operator, $second, 'right');
  365. }
  366. /**
  367. * Add a "right join where" clause to the query.
  368. *
  369. * @param string $table
  370. * @param string $one
  371. * @param string $operator
  372. * @param string $two
  373. * @return \Illuminate\Database\Query\Builder|static
  374. */
  375. public function rightJoinWhere($table, $one, $operator, $two)
  376. {
  377. return $this->joinWhere($table, $one, $operator, $two, 'right');
  378. }
  379. /**
  380. * Add a "cross join" clause to the query.
  381. *
  382. * @param string $table
  383. * @param string $first
  384. * @param string $operator
  385. * @param string $second
  386. * @return \Illuminate\Database\Query\Builder|static
  387. */
  388. public function crossJoin($table, $first = null, $operator = null, $second = null)
  389. {
  390. if ($first) {
  391. return $this->join($table, $first, $operator, $second, 'cross');
  392. }
  393. $this->joins[] = new JoinClause('cross', $table);
  394. return $this;
  395. }
  396. /**
  397. * Apply the callback's query changes if the given "value" is true.
  398. *
  399. * @param bool $value
  400. * @param \Closure $callback
  401. * @return \Illuminate\Database\Query\Builder
  402. */
  403. public function when($value, $callback)
  404. {
  405. $builder = $this;
  406. if ($value) {
  407. $builder = call_user_func($callback, $builder);
  408. }
  409. return $builder;
  410. }
  411. /**
  412. * Add a basic where clause to the query.
  413. *
  414. * @param string|array|\Closure $column
  415. * @param string $operator
  416. * @param mixed $value
  417. * @param string $boolean
  418. * @return $this
  419. *
  420. * @throws \InvalidArgumentException
  421. */
  422. public function where($column, $operator = null, $value = null, $boolean = 'and')
  423. {
  424. // If the column is an array, we will assume it is an array of key-value pairs
  425. // and can add them each as a where clause. We will maintain the boolean we
  426. // received when the method was called and pass it into the nested where.
  427. if (is_array($column)) {
  428. return $this->addArrayOfWheres($column, $boolean);
  429. }
  430. // Here we will make some assumptions about the operator. If only 2 values are
  431. // passed to the method, we will assume that the operator is an equals sign
  432. // and keep going. Otherwise, we'll require the operator to be passed in.
  433. if (func_num_args() == 2) {
  434. list($value, $operator) = [$operator, '='];
  435. } elseif ($this->invalidOperatorAndValue($operator, $value)) {
  436. throw new InvalidArgumentException('Illegal operator and value combination.');
  437. }
  438. // If the columns is actually a Closure instance, we will assume the developer
  439. // wants to begin a nested where statement which is wrapped in parenthesis.
  440. // We'll add that Closure to the query then return back out immediately.
  441. if ($column instanceof Closure) {
  442. return $this->whereNested($column, $boolean);
  443. }
  444. // If the given operator is not found in the list of valid operators we will
  445. // assume that the developer is just short-cutting the '=' operators and
  446. // we will set the operators to '=' and set the values appropriately.
  447. if (! in_array(strtolower($operator), $this->operators, true) &&
  448. ! in_array(strtolower($operator), $this->grammar->getOperators(), true)) {
  449. list($value, $operator) = [$operator, '='];
  450. }
  451. // If the value is a Closure, it means the developer is performing an entire
  452. // sub-select within the query and we will need to compile the sub-select
  453. // within the where clause to get the appropriate query record results.
  454. if ($value instanceof Closure) {
  455. return $this->whereSub($column, $operator, $value, $boolean);
  456. }
  457. // If the value is "null", we will just assume the developer wants to add a
  458. // where null clause to the query. So, we will allow a short-cut here to
  459. // that method for convenience so the developer doesn't have to check.
  460. if (is_null($value)) {
  461. return $this->whereNull($column, $boolean, $operator != '=');
  462. }
  463. // Now that we are working with just a simple query we can put the elements
  464. // in our array and add the query binding to our array of bindings that
  465. // will be bound to each SQL statements when it is finally executed.
  466. $type = 'Basic';
  467. $this->wheres[] = compact('type', 'column', 'operator', 'value', 'boolean');
  468. if (! $value instanceof Expression) {
  469. $this->addBinding($value, 'where');
  470. }
  471. return $this;
  472. }
  473. /**
  474. * Add an array of where clauses to the query.
  475. *
  476. * @param array $column
  477. * @param string $boolean
  478. * @param string $method
  479. * @return $this
  480. */
  481. protected function addArrayOfWheres($column, $boolean, $method = 'where')
  482. {
  483. return $this->whereNested(function ($query) use ($column, $method) {
  484. foreach ($column as $key => $value) {
  485. if (is_numeric($key) && is_array($value)) {
  486. call_user_func_array([$query, $method], $value);
  487. } else {
  488. $query->$method($key, '=', $value);
  489. }
  490. }
  491. }, $boolean);
  492. }
  493. /**
  494. * Determine if the given operator and value combination is legal.
  495. *
  496. * @param string $operator
  497. * @param mixed $value
  498. * @return bool
  499. */
  500. protected function invalidOperatorAndValue($operator, $value)
  501. {
  502. $isOperator = in_array($operator, $this->operators);
  503. return is_null($value) && $isOperator && ! in_array($operator, ['=', '<>', '!=']);
  504. }
  505. /**
  506. * Add an "or where" clause to the query.
  507. *
  508. * @param string $column
  509. * @param string $operator
  510. * @param mixed $value
  511. * @return \Illuminate\Database\Query\Builder|static
  512. */
  513. public function orWhere($column, $operator = null, $value = null)
  514. {
  515. return $this->where($column, $operator, $value, 'or');
  516. }
  517. /**
  518. * Add a "where" clause comparing two columns to the query.
  519. *
  520. * @param string|array $first
  521. * @param string|null $operator
  522. * @param string|null $second
  523. * @param string|null $boolean
  524. * @return \Illuminate\Database\Query\Builder|static
  525. */
  526. public function whereColumn($first, $operator = null, $second = null, $boolean = 'and')
  527. {
  528. // If the column is an array, we will assume it is an array of key-value pairs
  529. // and can add them each as a where clause. We will maintain the boolean we
  530. // received when the method was called and pass it into the nested where.
  531. if (is_array($first)) {
  532. return $this->addArrayOfWheres($first, $boolean, 'whereColumn');
  533. }
  534. // If the given operator is not found in the list of valid operators we will
  535. // assume that the developer is just short-cutting the '=' operators and
  536. // we will set the operators to '=' and set the values appropriately.
  537. if (! in_array(strtolower($operator), $this->operators, true) &&
  538. ! in_array(strtolower($operator), $this->grammar->getOperators(), true)) {
  539. list($second, $operator) = [$operator, '='];
  540. }
  541. $type = 'Column';
  542. $this->wheres[] = compact('type', 'first', 'operator', 'second', 'boolean');
  543. return $this;
  544. }
  545. /**
  546. * Add an "or where" clause comparing two columns to the query.
  547. *
  548. * @param string|array $first
  549. * @param string|null $operator
  550. * @param string|null $second
  551. * @return \Illuminate\Database\Query\Builder|static
  552. */
  553. public function orWhereColumn($first, $operator = null, $second = null)
  554. {
  555. return $this->whereColumn($first, $operator, $second, 'or');
  556. }
  557. /**
  558. * Add a raw where clause to the query.
  559. *
  560. * @param string $sql
  561. * @param array $bindings
  562. * @param string $boolean
  563. * @return $this
  564. */
  565. public function whereRaw($sql, array $bindings = [], $boolean = 'and')
  566. {
  567. $type = 'raw';
  568. $this->wheres[] = compact('type', 'sql', 'boolean');
  569. $this->addBinding($bindings, 'where');
  570. return $this;
  571. }
  572. /**
  573. * Add a raw or where clause to the query.
  574. *
  575. * @param string $sql
  576. * @param array $bindings
  577. * @return \Illuminate\Database\Query\Builder|static
  578. */
  579. public function orWhereRaw($sql, array $bindings = [])
  580. {
  581. return $this->whereRaw($sql, $bindings, 'or');
  582. }
  583. /**
  584. * Add a where between statement to the query.
  585. *
  586. * @param string $column
  587. * @param array $values
  588. * @param string $boolean
  589. * @param bool $not
  590. * @return $this
  591. */
  592. public function whereBetween($column, array $values, $boolean = 'and', $not = false)
  593. {
  594. $type = 'between';
  595. $this->wheres[] = compact('column', 'type', 'boolean', 'not');
  596. $this->addBinding($values, 'where');
  597. return $this;
  598. }
  599. /**
  600. * Add an or where between statement to the query.
  601. *
  602. * @param string $column
  603. * @param array $values
  604. * @return \Illuminate\Database\Query\Builder|static
  605. */
  606. public function orWhereBetween($column, array $values)
  607. {
  608. return $this->whereBetween($column, $values, 'or');
  609. }
  610. /**
  611. * Add a where not between statement to the query.
  612. *
  613. * @param string $column
  614. * @param array $values
  615. * @param string $boolean
  616. * @return \Illuminate\Database\Query\Builder|static
  617. */
  618. public function whereNotBetween($column, array $values, $boolean = 'and')
  619. {
  620. return $this->whereBetween($column, $values, $boolean, true);
  621. }
  622. /**
  623. * Add an or where not between statement to the query.
  624. *
  625. * @param string $column
  626. * @param array $values
  627. * @return \Illuminate\Database\Query\Builder|static
  628. */
  629. public function orWhereNotBetween($column, array $values)
  630. {
  631. return $this->whereNotBetween($column, $values, 'or');
  632. }
  633. /**
  634. * Add a nested where statement to the query.
  635. *
  636. * @param \Closure $callback
  637. * @param string $boolean
  638. * @return \Illuminate\Database\Query\Builder|static
  639. */
  640. public function whereNested(Closure $callback, $boolean = 'and')
  641. {
  642. $query = $this->forNestedWhere();
  643. call_user_func($callback, $query);
  644. return $this->addNestedWhereQuery($query, $boolean);
  645. }
  646. /**
  647. * Create a new query instance for nested where condition.
  648. *
  649. * @return \Illuminate\Database\Query\Builder
  650. */
  651. public function forNestedWhere()
  652. {
  653. $query = $this->newQuery();
  654. return $query->from($this->from);
  655. }
  656. /**
  657. * Add another query builder as a nested where to the query builder.
  658. *
  659. * @param \Illuminate\Database\Query\Builder|static $query
  660. * @param string $boolean
  661. * @return $this
  662. */
  663. public function addNestedWhereQuery($query, $boolean = 'and')
  664. {
  665. if (count($query->wheres)) {
  666. $type = 'Nested';
  667. $this->wheres[] = compact('type', 'query', 'boolean');
  668. $this->addBinding($query->getBindings(), 'where');
  669. }
  670. return $this;
  671. }
  672. /**
  673. * Add a full sub-select to the query.
  674. *
  675. * @param string $column
  676. * @param string $operator
  677. * @param \Closure $callback
  678. * @param string $boolean
  679. * @return $this
  680. */
  681. protected function whereSub($column, $operator, Closure $callback, $boolean)
  682. {
  683. $type = 'Sub';
  684. $query = $this->newQuery();
  685. // Once we have the query instance we can simply execute it so it can add all
  686. // of the sub-select's conditions to itself, and then we can cache it off
  687. // in the array of where clauses for the "main" parent query instance.
  688. call_user_func($callback, $query);
  689. $this->wheres[] = compact('type', 'column', 'operator', 'query', 'boolean');
  690. $this->addBinding($query->getBindings(), 'where');
  691. return $this;
  692. }
  693. /**
  694. * Add an exists clause to the query.
  695. *
  696. * @param \Closure $callback
  697. * @param string $boolean
  698. * @param bool $not
  699. * @return $this
  700. */
  701. public function whereExists(Closure $callback, $boolean = 'and', $not = false)
  702. {
  703. $query = $this->newQuery();
  704. // Similar to the sub-select clause, we will create a new query instance so
  705. // the developer may cleanly specify the entire exists query and we will
  706. // compile the whole thing in the grammar and insert it into the SQL.
  707. call_user_func($callback, $query);
  708. return $this->addWhereExistsQuery($query, $boolean, $not);
  709. }
  710. /**
  711. * Add an or exists clause to the query.
  712. *
  713. * @param \Closure $callback
  714. * @param bool $not
  715. * @return \Illuminate\Database\Query\Builder|static
  716. */
  717. public function orWhereExists(Closure $callback, $not = false)
  718. {
  719. return $this->whereExists($callback, 'or', $not);
  720. }
  721. /**
  722. * Add a where not exists clause to the query.
  723. *
  724. * @param \Closure $callback
  725. * @param string $boolean
  726. * @return \Illuminate\Database\Query\Builder|static
  727. */
  728. public function whereNotExists(Closure $callback, $boolean = 'and')
  729. {
  730. return $this->whereExists($callback, $boolean, true);
  731. }
  732. /**
  733. * Add a where not exists clause to the query.
  734. *
  735. * @param \Closure $callback
  736. * @return \Illuminate\Database\Query\Builder|static
  737. */
  738. public function orWhereNotExists(Closure $callback)
  739. {
  740. return $this->orWhereExists($callback, true);
  741. }
  742. /**
  743. * Add an exists clause to the query.
  744. *
  745. * @param \Illuminate\Database\Query\Builder $query
  746. * @param string $boolean
  747. * @param bool $not
  748. * @return $this
  749. */
  750. public function addWhereExistsQuery(Builder $query, $boolean = 'and', $not = false)
  751. {
  752. $type = $not ? 'NotExists' : 'Exists';
  753. $this->wheres[] = compact('type', 'operator', 'query', 'boolean');
  754. $this->addBinding($query->getBindings(), 'where');
  755. return $this;
  756. }
  757. /**
  758. * Add a "where in" clause to the query.
  759. *
  760. * @param string $column
  761. * @param mixed $values
  762. * @param string $boolean
  763. * @param bool $not
  764. * @return $this
  765. */
  766. public function whereIn($column, $values, $boolean = 'and', $not = false)
  767. {
  768. $type = $not ? 'NotIn' : 'In';
  769. if ($values instanceof static) {
  770. return $this->whereInExistingQuery(
  771. $column, $values, $boolean, $not
  772. );
  773. }
  774. // If the value of the where in clause is actually a Closure, we will assume that
  775. // the developer is using a full sub-select for this "in" statement, and will
  776. // execute those Closures, then we can re-construct the entire sub-selects.
  777. if ($values instanceof Closure) {
  778. return $this->whereInSub($column, $values, $boolean, $not);
  779. }
  780. if ($values instanceof Arrayable) {
  781. $values = $values->toArray();
  782. }
  783. $this->wheres[] = compact('type', 'column', 'values', 'boolean');
  784. $this->addBinding($values, 'where');
  785. return $this;
  786. }
  787. /**
  788. * Add an "or where in" clause to the query.
  789. *
  790. * @param string $column
  791. * @param mixed $values
  792. * @return \Illuminate\Database\Query\Builder|static
  793. */
  794. public function orWhereIn($column, $values)
  795. {
  796. return $this->whereIn($column, $values, 'or');
  797. }
  798. /**
  799. * Add a "where not in" clause to the query.
  800. *
  801. * @param string $column
  802. * @param mixed $values
  803. * @param string $boolean
  804. * @return \Illuminate\Database\Query\Builder|static
  805. */
  806. public function whereNotIn($column, $values, $boolean = 'and')
  807. {
  808. return $this->whereIn($column, $values, $boolean, true);
  809. }
  810. /**
  811. * Add an "or where not in" clause to the query.
  812. *
  813. * @param string $column
  814. * @param mixed $values
  815. * @return \Illuminate\Database\Query\Builder|static
  816. */
  817. public function orWhereNotIn($column, $values)
  818. {
  819. return $this->whereNotIn($column, $values, 'or');
  820. }
  821. /**
  822. * Add a where in with a sub-select to the query.
  823. *
  824. * @param string $column
  825. * @param \Closure $callback
  826. * @param string $boolean
  827. * @param bool $not
  828. * @return $this
  829. */
  830. protected function whereInSub($column, Closure $callback, $boolean, $not)
  831. {
  832. $type = $not ? 'NotInSub' : 'InSub';
  833. // To create the exists sub-select, we will actually create a query and call the
  834. // provided callback with the query so the developer may set any of the query
  835. // conditions they want for the in clause, then we'll put it in this array.
  836. call_user_func($callback, $query = $this->newQuery());
  837. $this->wheres[] = compact('type', 'column', 'query', 'boolean');
  838. $this->addBinding($query->getBindings(), 'where');
  839. return $this;
  840. }
  841. /**
  842. * Add a external sub-select to the query.
  843. *
  844. * @param string $column
  845. * @param \Illuminate\Database\Query\Builder|static $query
  846. * @param string $boolean
  847. * @param bool $not
  848. * @return $this
  849. */
  850. protected function whereInExistingQuery($column, $query, $boolean, $not)
  851. {
  852. $type = $not ? 'NotInSub' : 'InSub';
  853. $this->wheres[] = compact('type', 'column', 'query', 'boolean');
  854. $this->addBinding($query->getBindings(), 'where');
  855. return $this;
  856. }
  857. /**
  858. * Add a "where null" clause to the query.
  859. *
  860. * @param string $column
  861. * @param string $boolean
  862. * @param bool $not
  863. * @return $this
  864. */
  865. public function whereNull($column, $boolean = 'and', $not = false)
  866. {
  867. $type = $not ? 'NotNull' : 'Null';
  868. $this->wheres[] = compact('type', 'column', 'boolean');
  869. return $this;
  870. }
  871. /**
  872. * Add an "or where null" clause to the query.
  873. *
  874. * @param string $column
  875. * @return \Illuminate\Database\Query\Builder|static
  876. */
  877. public function orWhereNull($column)
  878. {
  879. return $this->whereNull($column, 'or');
  880. }
  881. /**
  882. * Add a "where not null" clause to the query.
  883. *
  884. * @param string $column
  885. * @param string $boolean
  886. * @return \Illuminate\Database\Query\Builder|static
  887. */
  888. public function whereNotNull($column, $boolean = 'and')
  889. {
  890. return $this->whereNull($column, $boolean, true);
  891. }
  892. /**
  893. * Add an "or where not null" clause to the query.
  894. *
  895. * @param string $column
  896. * @return \Illuminate\Database\Query\Builder|static
  897. */
  898. public function orWhereNotNull($column)
  899. {
  900. return $this->whereNotNull($column, 'or');
  901. }
  902. /**
  903. * Add a "where date" statement to the query.
  904. *
  905. * @param string $column
  906. * @param string $operator
  907. * @param int $value
  908. * @param string $boolean
  909. * @return \Illuminate\Database\Query\Builder|static
  910. */
  911. public function whereDate($column, $operator, $value, $boolean = 'and')
  912. {
  913. return $this->addDateBasedWhere('Date', $column, $operator, $value, $boolean);
  914. }
  915. /**
  916. * Add an "or where date" statement to the query.
  917. *
  918. * @param string $column
  919. * @param string $operator
  920. * @param int $value
  921. * @return \Illuminate\Database\Query\Builder|static
  922. */
  923. public function orWhereDate($column, $operator, $value)
  924. {
  925. return $this->whereDate($column, $operator, $value, 'or');
  926. }
  927. /**
  928. * Add a "where day" statement to the query.
  929. *
  930. * @param string $column
  931. * @param string $operator
  932. * @param int $value
  933. * @param string $boolean
  934. * @return \Illuminate\Database\Query\Builder|static
  935. */
  936. public function whereDay($column, $operator, $value, $boolean = 'and')
  937. {
  938. return $this->addDateBasedWhere('Day', $column, $operator, $value, $boolean);
  939. }
  940. /**
  941. * Add a "where month" statement to the query.
  942. *
  943. * @param string $column
  944. * @param string $operator
  945. * @param int $value
  946. * @param string $boolean
  947. * @return \Illuminate\Database\Query\Builder|static
  948. */
  949. public function whereMonth($column, $operator, $value, $boolean = 'and')
  950. {
  951. return $this->addDateBasedWhere('Month', $column, $operator, $value, $boolean);
  952. }
  953. /**
  954. * Add a "where year" statement to the query.
  955. *
  956. * @param string $column
  957. * @param string $operator
  958. * @param int $value
  959. * @param string $boolean
  960. * @return \Illuminate\Database\Query\Builder|static
  961. */
  962. public function whereYear($column, $operator, $value, $boolean = 'and')
  963. {
  964. return $this->addDateBasedWhere('Year', $column, $operator, $value, $boolean);
  965. }
  966. /**
  967. * Add a date based (year, month, day) statement to the query.
  968. *
  969. * @param string $type
  970. * @param string $column
  971. * @param string $operator
  972. * @param int $value
  973. * @param string $boolean
  974. * @return $this
  975. */
  976. protected function addDateBasedWhere($type, $column, $operator, $value, $boolean = 'and')
  977. {
  978. $this->wheres[] = compact('column', 'type', 'boolean', 'operator', 'value');
  979. $this->addBinding($value, 'where');
  980. return $this;
  981. }
  982. /**
  983. * Handles dynamic "where" clauses to the query.
  984. *
  985. * @param string $method
  986. * @param string $parameters
  987. * @return $this
  988. */
  989. public function dynamicWhere($method, $parameters)
  990. {
  991. $finder = substr($method, 5);
  992. $segments = preg_split('/(And|Or)(?=[A-Z])/', $finder, -1, PREG_SPLIT_DELIM_CAPTURE);
  993. // The connector variable will determine which connector will be used for the
  994. // query condition. We will change it as we come across new boolean values
  995. // in the dynamic method strings, which could contain a number of these.
  996. $connector = 'and';
  997. $index = 0;
  998. foreach ($segments as $segment) {
  999. // If the segment is not a boolean connector, we can assume it is a column's name
  1000. // and we will add it to the query as a new constraint as a where clause, then
  1001. // we can keep iterating through the dynamic method string's segments again.
  1002. if ($segment != 'And' && $segment != 'Or') {
  1003. $this->addDynamic($segment, $connector, $parameters, $index);
  1004. $index++;
  1005. }
  1006. // Otherwise, we will store the connector so we know how the next where clause we
  1007. // find in the query should be connected to the previous ones, meaning we will
  1008. // have the proper boolean connector to connect the next where clause found.
  1009. else {
  1010. $connector = $segment;
  1011. }
  1012. }
  1013. return $this;
  1014. }
  1015. /**
  1016. * Add a single dynamic where clause statement to the query.
  1017. *
  1018. * @param string $segment
  1019. * @param string $connector
  1020. * @param array $parameters
  1021. * @param int $index
  1022. * @return void
  1023. */
  1024. protected function addDynamic($segment, $connector, $parameters, $index)
  1025. {
  1026. // Once we have parsed out the columns and formatted the boolean operators we
  1027. // are ready to add it to this query as a where clause just like any other
  1028. // clause on the query. Then we'll increment the parameter index values.
  1029. $bool = strtolower($connector);
  1030. $this->where(Str::snake($segment), '=', $parameters[$index], $bool);
  1031. }
  1032. /**
  1033. * Add a "group by" clause to the query.
  1034. *
  1035. * @param array|string $column,...
  1036. * @return $this
  1037. */
  1038. public function groupBy()
  1039. {
  1040. foreach (func_get_args() as $arg) {
  1041. $this->groups = array_merge((array) $this->groups, is_array($arg) ? $arg : [$arg]);
  1042. }
  1043. return $this;
  1044. }
  1045. /**
  1046. * Add a "having" clause to the query.
  1047. *
  1048. * @param string $column
  1049. * @param string $operator
  1050. * @param string $value
  1051. * @param string $boolean
  1052. * @return $this
  1053. */
  1054. public function having($column, $operator = null, $value = null, $boolean = 'and')
  1055. {
  1056. $type = 'basic';
  1057. $this->havings[] = compact('type', 'column', 'operator', 'value', 'boolean');
  1058. if (! $value instanceof Expression) {
  1059. $this->addBinding($value, 'having');
  1060. }
  1061. return $this;
  1062. }
  1063. /**
  1064. * Add a "or having" clause to the query.
  1065. *
  1066. * @param string $column
  1067. * @param string $operator
  1068. * @param string $value
  1069. * @return \Illuminate\Database\Query\Builder|static
  1070. */
  1071. public function orHaving($column, $operator = null, $value = null)
  1072. {
  1073. return $this->having($column, $operator, $value, 'or');
  1074. }
  1075. /**
  1076. * Add a raw having clause to the query.
  1077. *
  1078. * @param string $sql
  1079. * @param array $bindings
  1080. * @param string $boolean
  1081. * @return $this
  1082. */
  1083. public function havingRaw($sql, array $bindings = [], $boolean = 'and')
  1084. {
  1085. $type = 'raw';
  1086. $this->havings[] = compact('type', 'sql', 'boolean');
  1087. $this->addBinding($bindings, 'having');
  1088. return $this;
  1089. }
  1090. /**
  1091. * Add a raw or having clause to the query.
  1092. *
  1093. * @param string $sql
  1094. * @param array $bindings
  1095. * @return \Illuminate\Database\Query\Builder|static
  1096. */
  1097. public function orHavingRaw($sql, array $bindings = [])
  1098. {
  1099. return $this->havingRaw($sql, $bindings, 'or');
  1100. }
  1101. /**
  1102. * Add an "order by" clause to the query.
  1103. *
  1104. * @param string $column
  1105. * @param string $direction
  1106. * @return $this
  1107. */
  1108. public function orderBy($column, $direction = 'asc')
  1109. {
  1110. $property = $this->unions ? 'unionOrders' : 'orders';
  1111. $direction = strtolower($direction) == 'asc' ? 'asc' : 'desc';
  1112. $this->{$property}[] = compact('column', 'direction');
  1113. return $this;
  1114. }
  1115. /**
  1116. * Add an "order by" clause for a timestamp to the query.
  1117. *
  1118. * @param string $column
  1119. * @return \Illuminate\Database\Query\Builder|static
  1120. */
  1121. public function latest($column = 'created_at')
  1122. {
  1123. return $this->orderBy($column, 'desc');
  1124. }
  1125. /**
  1126. * Add an "order by" clause for a timestamp to the query.
  1127. *
  1128. * @param string $column
  1129. * @return \Illuminate\Database\Query\Builder|static
  1130. */
  1131. public function oldest($column = 'created_at')
  1132. {
  1133. return $this->orderBy($column, 'asc');
  1134. }
  1135. /**
  1136. * Add a raw "order by" clause to the query.
  1137. *
  1138. * @param string $sql
  1139. * @param array $bindings
  1140. * @return $this
  1141. */
  1142. public function orderByRaw($sql, $bindings = [])
  1143. {
  1144. $property = $this->unions ? 'unionOrders' : 'orders';
  1145. $type = 'raw';
  1146. $this->{$property}[] = compact('type', 'sql');
  1147. $this->addBinding($bindings, 'order');
  1148. return $this;
  1149. }
  1150. /**
  1151. * Set the "offset" value of the query.
  1152. *
  1153. * @param int $value
  1154. * @return $this
  1155. */
  1156. public function offset($value)
  1157. {
  1158. $property = $this->unions ? 'unionOffset' : 'offset';
  1159. $this->$property = max(0, $value);
  1160. return $this;
  1161. }
  1162. /**
  1163. * Alias to set the "offset" value of the query.
  1164. *
  1165. * @param int $value
  1166. * @return \Illuminate\Database\Query\Builder|static
  1167. */
  1168. public function skip($value)
  1169. {
  1170. return $this->offset($value);
  1171. }
  1172. /**
  1173. * Set the "limit" value of the query.
  1174. *
  1175. * @param int $value
  1176. * @return $this
  1177. */
  1178. public function limit($value)
  1179. {
  1180. $property = $this->unions ? 'unionLimit' : 'limit';
  1181. if ($value >= 0) {
  1182. $this->$property = $value;
  1183. }
  1184. return $this;
  1185. }
  1186. /**
  1187. * Alias to set the "limit" value of the query.
  1188. *
  1189. * @param int $value
  1190. * @return \Illuminate\Database\Query\Builder|static
  1191. */
  1192. public function take($value)
  1193. {
  1194. return $this->limit($value);
  1195. }
  1196. /**
  1197. * Set the limit and offset for a given page.
  1198. *
  1199. * @param int $page
  1200. * @param int $perPage
  1201. * @return \Illuminate\Database\Query\Builder|static
  1202. */
  1203. public function forPage($page, $perPage = 15)
  1204. {
  1205. return $this->skip(($page - 1) * $perPage)->take($perPage);
  1206. }
  1207. /**
  1208. * Constrain the query to the next "page" of results after a given ID.
  1209. *
  1210. * @param int $perPage
  1211. * @param int $lastId
  1212. * @param string $column
  1213. * @return \Illuminate\Database\Query\Builder|static
  1214. */
  1215. public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id')
  1216. {
  1217. return $this->where($column, '>', $lastId)
  1218. ->orderBy($column, 'asc')
  1219. ->take($perPage);
  1220. }
  1221. /**
  1222. * Add a union statement to the query.
  1223. *
  1224. * @param \Illuminate\Database\Query\Builder|\Closure $query
  1225. * @param bool $all
  1226. * @return \Illuminate\Database\Query\Builder|static
  1227. */
  1228. public function union($query, $all = false)
  1229. {
  1230. if ($query instanceof Closure) {
  1231. call_user_func($query, $query = $this->newQuery());
  1232. }
  1233. $this->unions[] = compact('query', 'all');
  1234. $this->addBinding($query->getBindings(), 'union');
  1235. return $this;
  1236. }
  1237. /**
  1238. * Add a union all statement to the query.
  1239. *
  1240. * @param \Illuminate\Database\Query\Builder|\Closure $query
  1241. * @return \Illuminate\Database\Query\Builder|static
  1242. */
  1243. public function unionAll($query)
  1244. {
  1245. return $this->union($query, true);
  1246. }
  1247. /**
  1248. * Lock the selected rows in the table.
  1249. *
  1250. * @param bool $value
  1251. * @return $this
  1252. */
  1253. public function lock($value = true)
  1254. {
  1255. $this->lock = $value;
  1256. if ($this->lock) {
  1257. $this->useWritePdo();
  1258. }
  1259. return $this;
  1260. }
  1261. /**
  1262. * Lock the selected rows in the table for updating.
  1263. *
  1264. * @return \Illuminate\Database\Query\Builder
  1265. */
  1266. public function lockForUpdate()
  1267. {
  1268. return $this->lock(true);
  1269. }
  1270. /**
  1271. * Share lock the selected rows in the table.
  1272. *
  1273. * @return \Illuminate\Database\Query\Builder
  1274. */
  1275. public function sharedLock()
  1276. {
  1277. return $this->lock(false);
  1278. }
  1279. /**
  1280. * Get the SQL representation of the query.
  1281. *
  1282. * @return string
  1283. */
  1284. public function toSql()
  1285. {
  1286. return $this->grammar->compileSelect($this);
  1287. }
  1288. /**
  1289. * Execute a query for a single record by ID.
  1290. *
  1291. * @param int $id
  1292. * @param array $columns
  1293. * @return mixed|static
  1294. */
  1295. public function find($id, $columns = ['*'])
  1296. {
  1297. return $this->where('id', '=', $id)->first($columns);
  1298. }
  1299. /**
  1300. * Get a single column's value from the first result of a query.
  1301. *
  1302. * @param string $column
  1303. * @return mixed
  1304. */
  1305. public function value($column)
  1306. {
  1307. $result = (array) $this->first([$column]);
  1308. return count($result) > 0 ? reset($result) : null;
  1309. }
  1310. /**
  1311. * Execute the query and get the first result.
  1312. *
  1313. * @param array $columns
  1314. * @return mixed|static
  1315. */
  1316. public function first($columns = ['*'])
  1317. {
  1318. $results = $this->take(1)->get($columns);
  1319. return count($results) > 0 ? reset($results) : null;
  1320. }
  1321. /**
  1322. * Execute the query as a "select" statement.
  1323. *
  1324. * @param array $columns
  1325. * @return array|static[]
  1326. */
  1327. public function get($columns = ['*'])
  1328. {
  1329. $original = $this->columns;
  1330. if (is_null($original)) {
  1331. $this->columns = $columns;
  1332. }
  1333. $results = $this->processor->processSelect($this, $this->runSelect());
  1334. $this->columns = $original;
  1335. return $results;
  1336. }
  1337. /**
  1338. * Run the query as a "select" statement against the connection.
  1339. *
  1340. * @return array
  1341. */
  1342. protected function runSelect()
  1343. {
  1344. return $this->connection->select($this->toSql(), $this->getBindings(), ! $this->useWritePdo);
  1345. }
  1346. /**
  1347. * Paginate the given query into a simple paginator.
  1348. *
  1349. * @param int $perPage
  1350. * @param array $columns
  1351. * @param string $pageName
  1352. * @param int|null $page
  1353. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  1354. */
  1355. public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
  1356. {
  1357. $page = $page ?: Paginator::resolveCurrentPage($pageName);
  1358. $total = $this->getCountForPagination($columns);
  1359. $results = $this->forPage($page, $perPage)->get($columns);
  1360. return new LengthAwarePaginator($results, $total, $perPage, $page, [
  1361. 'path' => Paginator::resolveCurrentPath(),
  1362. 'pageName' => $pageName,
  1363. ]);
  1364. }
  1365. /**
  1366. * Get a paginator only supporting simple next and previous links.
  1367. *
  1368. * This is more efficient on larger data-sets, etc.
  1369. *
  1370. * @param int $perPage
  1371. * @param array $columns
  1372. * @param string $pageName
  1373. * @param int|null $page
  1374. * @return \Illuminate\Contracts\Pagination\Paginator
  1375. */
  1376. public function simplePaginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
  1377. {
  1378. $page = $page ?: Paginator::resolveCurrentPage($pageName);
  1379. $this->skip(($page - 1) * $perPage)->take($perPage + 1);
  1380. return new Paginator($this->get($columns), $perPage, $page, [
  1381. 'path' => Paginator::resolveCurrentPath(),
  1382. 'pageName' => $pageName,
  1383. ]);
  1384. }
  1385. /**
  1386. * Get the count of the total records for the paginator.
  1387. *
  1388. * @param array $columns
  1389. * @return int
  1390. */
  1391. public function getCountForPagination($columns = ['*'])
  1392. {
  1393. $this->backupFieldsForCount();
  1394. $this->aggregate = ['function' => 'count', 'columns' => $this->clearSelectAliases($columns)];
  1395. $results = $this->get();
  1396. $this->aggregate = null;
  1397. $this->restoreFieldsForCount();
  1398. if (isset($this->groups)) {
  1399. return count($results);
  1400. }
  1401. return isset($results[0]) ? (int) array_change_key_case((array) $results[0])['aggregate'] : 0;
  1402. }
  1403. /**
  1404. * Backup some fields for the pagination count.
  1405. *
  1406. * @return void
  1407. */
  1408. protected function backupFieldsForCount()
  1409. {
  1410. foreach (['orders', 'limit', 'offset', 'columns'] as $field) {
  1411. $this->backups[$field] = $this->{$field};
  1412. $this->{$field} = null;
  1413. }
  1414. foreach (['order', 'select'] as $key) {
  1415. $this->bindingBackups[$key] = $this->bindings[$key];
  1416. $this->bindings[$key] = [];
  1417. }
  1418. }
  1419. /**
  1420. * Remove the column aliases since they will break count queries.
  1421. *
  1422. * @param array $columns
  1423. * @return array
  1424. */
  1425. protected function clearSelectAliases(array $columns)
  1426. {
  1427. return array_map(function ($column) {
  1428. return is_string($column) && ($aliasPosition = strpos(strtolower($column), ' as ')) !== false
  1429. ? substr($column, 0, $aliasPosition) : $column;
  1430. }, $columns);
  1431. }
  1432. /**
  1433. * Restore some fields after the pagination count.
  1434. *
  1435. * @return void
  1436. */
  1437. protected function restoreFieldsForCount()
  1438. {
  1439. foreach (['orders', 'limit', 'offset', 'columns'] as $field) {
  1440. $this->{$field} = $this->backups[$field];
  1441. }
  1442. foreach (['order', 'select'] as $key) {
  1443. $this->bindings[$key] = $this->bindingBackups[$key];
  1444. }
  1445. $this->backups = [];
  1446. $this->bindingBackups = [];
  1447. }
  1448. /**
  1449. * Chunk the results of the query.
  1450. *
  1451. * @param int $count
  1452. * @param callable $callback
  1453. * @return bool
  1454. */
  1455. public function chunk($count, callable $callback)
  1456. {
  1457. $results = $this->forPage($page = 1, $count)->get();
  1458. while (count($results) > 0) {
  1459. // On each chunk result set, we will pass them to the callback and then let the
  1460. // developer take care of everything within the callback, which allows us to
  1461. // keep the memory low for spinning through large result sets for working.
  1462. if (call_user_func($callback, $results) === false) {
  1463. return false;
  1464. }
  1465. $page++;
  1466. $results = $this->forPage($page, $count)->get();
  1467. }
  1468. return true;
  1469. }
  1470. /**
  1471. * Chunk the results of a query by comparing numeric IDs.
  1472. *
  1473. * @param int $count
  1474. * @param callable $callback
  1475. * @param string $column
  1476. * @return bool
  1477. */
  1478. public function chunkById($count, callable $callback, $column = 'id')
  1479. {
  1480. $lastId = null;
  1481. $results = $this->forPageAfterId($count, 0, $column)->get();
  1482. while (! empty($results)) {
  1483. if (call_user_func($callback, $results) === false) {
  1484. return false;
  1485. }
  1486. $lastId = last($results)->{$column};
  1487. $results = $this->forPageAfterId($count, $lastId, $column)->get();
  1488. }
  1489. return true;
  1490. }
  1491. /**
  1492. * Execute a callback over each item while chunking.
  1493. *
  1494. * @param callable $callback
  1495. * @param int $count
  1496. * @return bool
  1497. *
  1498. * @throws \RuntimeException
  1499. */
  1500. public function each(callable $callback, $count = 1000)
  1501. {
  1502. if (is_null($this->orders) && is_null($this->unionOrders)) {
  1503. throw new RuntimeException('You must specify an orderBy clause when using the "each" function.');
  1504. }
  1505. return $this->chunk($count, function ($results) use ($callback) {
  1506. foreach ($results as $key => $value) {
  1507. if ($callback($value, $key) === false) {
  1508. return false;
  1509. }
  1510. }
  1511. });
  1512. }
  1513. /**
  1514. * Get an array with the values of a given column.
  1515. *
  1516. * @param string $column
  1517. * @param string|null $key
  1518. * @return array
  1519. */
  1520. public function pluck($column, $key = null)
  1521. {
  1522. $results = $this->get(is_null($key) ? [$column] : [$column, $key]);
  1523. // If the columns are qualified with a table or have an alias, we cannot use
  1524. // those directly in the "pluck" operations since the results from the DB
  1525. // are only keyed by the column itself. We'll strip the table out here.
  1526. return Arr::pluck(
  1527. $results,
  1528. $this->stripTableForPluck($column),
  1529. $this->stripTableForPluck($key)
  1530. );
  1531. }
  1532. /**
  1533. * Alias for the "pluck" method.
  1534. *
  1535. * @param string $column
  1536. * @param string|null $key
  1537. * @return array
  1538. *
  1539. * @deprecated since version 5.2. Use the "pluck" method directly.
  1540. */
  1541. public function lists($column, $key = null)
  1542. {
  1543. return $this->pluck($column, $key);
  1544. }
  1545. /**
  1546. * Strip off the table name or alias from a column identifier.
  1547. *
  1548. * @param string $column
  1549. * @return string|null
  1550. */
  1551. protected function stripTableForPluck($column)
  1552. {
  1553. return is_null($column) ? $column : last(preg_split('~\.| ~', $column));
  1554. }
  1555. /**
  1556. * Concatenate values of a given column as a string.
  1557. *
  1558. * @param string $column
  1559. * @param string $glue
  1560. * @return string
  1561. */
  1562. public function implode($column, $glue = '')
  1563. {
  1564. return implode($glue, $this->pluck($column));
  1565. }
  1566. /**
  1567. * Determine if any rows exist for the current query.
  1568. *
  1569. * @return bool
  1570. */
  1571. public function exists()
  1572. {
  1573. $sql = $this->grammar->compil