1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821 |
- <?php
-
- namespace Illuminate\Database\Query;
-
- use Closure;
- use RuntimeException;
- use BadMethodCallException;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Str;
- use InvalidArgumentException;
- use Illuminate\Support\Collection;
- use Illuminate\Pagination\Paginator;
- use Illuminate\Support\Traits\Macroable;
- use Illuminate\Contracts\Support\Arrayable;
- use Illuminate\Database\ConnectionInterface;
- use Illuminate\Database\Concerns\BuildsQueries;
- use Illuminate\Database\Query\Grammars\Grammar;
- use Illuminate\Database\Query\Processors\Processor;
- use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
-
- class Builder
- {
- use BuildsQueries, Macroable {
- __call as macroCall;
- }
-
- /**
- * The database connection instance.
- *
- * @var \Illuminate\Database\ConnectionInterface
- */
- public $connection;
-
- /**
- * The database query grammar instance.
- *
- * @var \Illuminate\Database\Query\Grammars\Grammar
- */
- public $grammar;
-
- /**
- * The database query post processor instance.
- *
- * @var \Illuminate\Database\Query\Processors\Processor
- */
- public $processor;
-
- /**
- * The current query value bindings.
- *
- * @var array
- */
- public $bindings = [
- 'select' => [],
- 'from' => [],
- 'join' => [],
- 'where' => [],
- 'having' => [],
- 'order' => [],
- 'union' => [],
- ];
-
- /**
- * An aggregate function and column to be run.
- *
- * @var array
- */
- public $aggregate;
-
- /**
- * The columns that should be returned.
- *
- * @var array
- */
- public $columns;
-
- /**
- * Indicates if the query returns distinct results.
- *
- * @var bool
- */
- public $distinct = false;
-
- /**
- * The table which the query is targeting.
- *
- * @var string
- */
- public $from;
-
- /**
- * The table joins for the query.
- *
- * @var array
- */
- public $joins;
-
- /**
- * The where constraints for the query.
- *
- * @var array
- */
- public $wheres = [];
-
- /**
- * The groupings for the query.
- *
- * @var array
- */
- public $groups;
-
- /**
- * The having constraints for the query.
- *
- * @var array
- */
- public $havings;
-
- /**
- * The orderings for the query.
- *
- * @var array
- */
- public $orders;
-
- /**
- * The maximum number of records to return.
- *
- * @var int
- */
- public $limit;
-
- /**
- * The number of records to skip.
- *
- * @var int
- */
- public $offset;
-
- /**
- * The query union statements.
- *
- * @var array
- */
- public $unions;
-
- /**
- * The maximum number of union records to return.
- *
- * @var int
- */
- public $unionLimit;
-
- /**
- * The number of union records to skip.
- *
- * @var int
- */
- public $unionOffset;
-
- /**
- * The orderings for the union query.
- *
- * @var array
- */
- public $unionOrders;
-
- /**
- * Indicates whether row locking is being used.
- *
- * @var string|bool
- */
- public $lock;
-
- /**
- * All of the available clause operators.
- *
- * @var array
- */
- public $operators = [
- '=', '<', '>', '<=', '>=', '<>', '!=', '<=>',
- 'like', 'like binary', 'not like', 'ilike',
- '&', '|', '^', '<<', '>>',
- 'rlike', 'regexp', 'not regexp',
- '~', '~*', '!~', '!~*', 'similar to',
- 'not similar to', 'not ilike', '~~*', '!~~*',
- ];
-
- /**
- * Whether use write pdo for select.
- *
- * @var bool
- */
- public $useWritePdo = false;
-
- /**
- * Create a new query builder instance.
- *
- * @param \Illuminate\Database\ConnectionInterface $connection
- * @param \Illuminate\Database\Query\Grammars\Grammar $grammar
- * @param \Illuminate\Database\Query\Processors\Processor $processor
- * @return void
- */
- public function __construct(ConnectionInterface $connection,
- Grammar $grammar = null,
- Processor $processor = null)
- {
- $this->connection = $connection;
- $this->grammar = $grammar ?: $connection->getQueryGrammar();
- $this->processor = $processor ?: $connection->getPostProcessor();
- }
-
- /**
- * Set the columns to be selected.
- *
- * @param array|mixed $columns
- * @return $this
- */
- public function select($columns = ['*'])
- {
- $this->columns = is_array($columns) ? $columns : func_get_args();
-
- return $this;
- }
-
- /**
- * Add a subselect expression to the query.
- *
- * @param \Closure|\Illuminate\Database\Query\Builder|string $query
- * @param string $as
- * @return \Illuminate\Database\Query\Builder|static
- *
- * @throws \InvalidArgumentException
- */
- public function selectSub($query, $as)
- {
- list($query, $bindings) = $this->createSub($query);
-
- return $this->selectRaw(
- '('.$query.') as '.$this->grammar->wrap($as), $bindings
- );
- }
-
- /**
- * Add a new "raw" select expression to the query.
- *
- * @param string $expression
- * @param array $bindings
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function selectRaw($expression, array $bindings = [])
- {
- $this->addSelect(new Expression($expression));
-
- if ($bindings) {
- $this->addBinding($bindings, 'select');
- }
-
- return $this;
- }
-
- /**
- * Makes "from" fetch from a subquery.
- *
- * @param \Closure|\Illuminate\Database\Query\Builder|string $query
- * @param string $as
- * @return \Illuminate\Database\Query\Builder|static
- *
- * @throws \InvalidArgumentException
- */
- public function fromSub($query, $as)
- {
- list($query, $bindings) = $this->createSub($query);
-
- return $this->fromRaw('('.$query.') as '.$this->grammar->wrap($as), $bindings);
- }
-
- /**
- * Add a raw from clause to the query.
- *
- * @param string $expression
- * @param mixed $bindings
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function fromRaw($expression, $bindings = [])
- {
- $this->from = new Expression($expression);
-
- $this->addBinding($bindings, 'from');
-
- return $this;
- }
-
- /**
- * Creates a subquery and parse it.
- *
- * @param \Closure|\Illuminate\Database\Query\Builder|string $query
- * @return array
- */
- protected function createSub($query)
- {
- // If the given query is a Closure, we will execute it while passing in a new
- // query instance to the Closure. This will give the developer a chance to
- // format and work with the query before we cast it to a raw SQL string.
- if ($query instanceof Closure) {
- $callback = $query;
-
- $callback($query = $this->forSubQuery());
- }
-
- return $this->parseSub($query);
- }
-
- /**
- * Parse the subquery into SQL and bindings.
- *
- * @param mixed $query
- * @return array
- */
- protected function parseSub($query)
- {
- if ($query instanceof self) {
- return [$query->toSql(), $query->getBindings()];
- } elseif (is_string($query)) {
- return [$query, []];
- } else {
- throw new InvalidArgumentException;
- }
- }
-
- /**
- * Add a new select column to the query.
- *
- * @param array|mixed $column
- * @return $this
- */
- public function addSelect($column)
- {
- $column = is_array($column) ? $column : func_get_args();
-
- $this->columns = array_merge((array) $this->columns, $column);
-
- return $this;
- }
-
- /**
- * Force the query to only return distinct results.
- *
- * @return $this
- */
- public function distinct()
- {
- $this->distinct = true;
-
- return $this;
- }
-
- /**
- * Set the table which the query is targeting.
- *
- * @param string $table
- * @return $this
- */
- public function from($table)
- {
- $this->from = $table;
-
- return $this;
- }
-
- /**
- * Add a join clause to the query.
- *
- * @param string $table
- * @param string $first
- * @param string|null $operator
- * @param string|null $second
- * @param string $type
- * @param bool $where
- * @return $this
- */
- public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false)
- {
- $join = new JoinClause($this, $type, $table);
-
- // If the first "column" of the join is really a Closure instance the developer
- // is trying to build a join with a complex "on" clause containing more than
- // one condition, so we'll add the join and call a Closure with the query.
- if ($first instanceof Closure) {
- call_user_func($first, $join);
-
- $this->joins[] = $join;
-
- $this->addBinding($join->getBindings(), 'join');
- }
-
- // If the column is simply a string, we can assume the join simply has a basic
- // "on" clause with a single condition. So we will just build the join with
- // this simple join clauses attached to it. There is not a join callback.
- else {
- $method = $where ? 'where' : 'on';
-
- $this->joins[] = $join->$method($first, $operator, $second);
-
- $this->addBinding($join->getBindings(), 'join');
- }
-
- return $this;
- }
-
- /**
- * Add a "join where" clause to the query.
- *
- * @param string $table
- * @param string $first
- * @param string $operator
- * @param string $second
- * @param string $type
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function joinWhere($table, $first, $operator, $second, $type = 'inner')
- {
- return $this->join($table, $first, $operator, $second, $type, true);
- }
-
- /**
- * Add a subquery join clause to the query.
- *
- * @param \Closure|\Illuminate\Database\Query\Builder|string $query
- * @param string $as
- * @param string $first
- * @param string|null $operator
- * @param string|null $second
- * @param string $type
- * @param bool $where
- * @return \Illuminate\Database\Query\Builder|static
- *
- * @throws \InvalidArgumentException
- */
- public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false)
- {
- list($query, $bindings) = $this->createSub($query);
-
- $expression = '('.$query.') as '.$this->grammar->wrap($as);
-
- $this->addBinding($bindings, 'join');
-
- return $this->join(new Expression($expression), $first, $operator, $second, $type, $where);
- }
-
- /**
- * Add a left join to the query.
- *
- * @param string $table
- * @param string $first
- * @param string|null $operator
- * @param string|null $second
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function leftJoin($table, $first, $operator = null, $second = null)
- {
- return $this->join($table, $first, $operator, $second, 'left');
- }
-
- /**
- * Add a "join where" clause to the query.
- *
- * @param string $table
- * @param string $first
- * @param string $operator
- * @param string $second
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function leftJoinWhere($table, $first, $operator, $second)
- {
- return $this->joinWhere($table, $first, $operator, $second, 'left');
- }
-
- /**
- * Add a subquery left join to the query.
- *
- * @param \Closure|\Illuminate\Database\Query\Builder|string $query
- * @param string $as
- * @param string $first
- * @param string|null $operator
- * @param string|null $second
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function leftJoinSub($query, $as, $first, $operator = null, $second = null)
- {
- return $this->joinSub($query, $as, $first, $operator, $second, 'left');
- }
-
- /**
- * Add a right join to the query.
- *
- * @param string $table
- * @param string $first
- * @param string|null $operator
- * @param string|null $second
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function rightJoin($table, $first, $operator = null, $second = null)
- {
- return $this->join($table, $first, $operator, $second, 'right');
- }
-
- /**
- * Add a "right join where" clause to the query.
- *
- * @param string $table
- * @param string $first
- * @param string $operator
- * @param string $second
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function rightJoinWhere($table, $first, $operator, $second)
- {
- return $this->joinWhere($table, $first, $operator, $second, 'right');
- }
-
- /**
- * Add a subquery right join to the query.
- *
- * @param \Closure|\Illuminate\Database\Query\Builder|string $query
- * @param string $as
- * @param string $first
- * @param string|null $operator
- * @param string|null $second
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function rightJoinSub($query, $as, $first, $operator = null, $second = null)
- {
- return $this->joinSub($query, $as, $first, $operator, $second, 'right');
- }
-
- /**
- * Add a "cross join" clause to the query.
- *
- * @param string $table
- * @param string|null $first
- * @param string|null $operator
- * @param string|null $second
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function crossJoin($table, $first = null, $operator = null, $second = null)
- {
- if ($first) {
- return $this->join($table, $first, $operator, $second, 'cross');
- }
-
- $this->joins[] = new JoinClause($this, 'cross', $table);
-
- return $this;
- }
-
- /**
- * Merge an array of where clauses and bindings.
- *
- * @param array $wheres
- * @param array $bindings
- * @return void
- */
- public function mergeWheres($wheres, $bindings)
- {
- $this->wheres = array_merge($this->wheres, (array) $wheres);
-
- $this->bindings['where'] = array_values(
- array_merge($this->bindings['where'], (array) $bindings)
- );
- }
-
- /**
- * Add a basic where clause to the query.
- *
- * @param string|array|\Closure $column
- * @param mixed $operator
- * @param mixed $value
- * @param string $boolean
- * @return $this
- */
- public function where($column, $operator = null, $value = null, $boolean = 'and')
- {
- // If the column is an array, we will assume it is an array of key-value pairs
- // and can add them each as a where clause. We will maintain the boolean we
- // received when the method was called and pass it into the nested where.
- if (is_array($column)) {
- return $this->addArrayOfWheres($column, $boolean);
- }
-
- // Here we will make some assumptions about the operator. If only 2 values are
- // passed to the method, we will assume that the operator is an equals sign
- // and keep going. Otherwise, we'll require the operator to be passed in.
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- // If the columns is actually a Closure instance, we will assume the developer
- // wants to begin a nested where statement which is wrapped in parenthesis.
- // We'll add that Closure to the query then return back out immediately.
- if ($column instanceof Closure) {
- return $this->whereNested($column, $boolean);
- }
-
- // If the given operator is not found in the list of valid operators we will
- // assume that the developer is just short-cutting the '=' operators and
- // we will set the operators to '=' and set the values appropriately.
- if ($this->invalidOperator($operator)) {
- list($value, $operator) = [$operator, '='];
- }
-
- // If the value is a Closure, it means the developer is performing an entire
- // sub-select within the query and we will need to compile the sub-select
- // within the where clause to get the appropriate query record results.
- if ($value instanceof Closure) {
- return $this->whereSub($column, $operator, $value, $boolean);
- }
-
- // If the value is "null", we will just assume the developer wants to add a
- // where null clause to the query. So, we will allow a short-cut here to
- // that method for convenience so the developer doesn't have to check.
- if (is_null($value)) {
- return $this->whereNull($column, $boolean, $operator !== '=');
- }
-
- // If the column is making a JSON reference we'll check to see if the value
- // is a boolean. If it is, we'll add the raw boolean string as an actual
- // value to the query to ensure this is properly handled by the query.
- if (Str::contains($column, '->') && is_bool($value)) {
- $value = new Expression($value ? 'true' : 'false');
- }
-
- // Now that we are working with just a simple query we can put the elements
- // in our array and add the query binding to our array of bindings that
- // will be bound to each SQL statements when it is finally executed.
- $type = 'Basic';
-
- $this->wheres[] = compact(
- 'type', 'column', 'operator', 'value', 'boolean'
- );
-
- if (! $value instanceof Expression) {
- $this->addBinding($value, 'where');
- }
-
- return $this;
- }
-
- /**
- * Add an array of where clauses to the query.
- *
- * @param array $column
- * @param string $boolean
- * @param string $method
- * @return $this
- */
- protected function addArrayOfWheres($column, $boolean, $method = 'where')
- {
- return $this->whereNested(function ($query) use ($column, $method, $boolean) {
- foreach ($column as $key => $value) {
- if (is_numeric($key) && is_array($value)) {
- $query->{$method}(...array_values($value));
- } else {
- $query->$method($key, '=', $value, $boolean);
- }
- }
- }, $boolean);
- }
-
- /**
- * Prepare the value and operator for a where clause.
- *
- * @param string $value
- * @param string $operator
- * @param bool $useDefault
- * @return array
- *
- * @throws \InvalidArgumentException
- */
- public function prepareValueAndOperator($value, $operator, $useDefault = false)
- {
- if ($useDefault) {
- return [$operator, '='];
- } elseif ($this->invalidOperatorAndValue($operator, $value)) {
- throw new InvalidArgumentException('Illegal operator and value combination.');
- }
-
- return [$value, $operator];
- }
-
- /**
- * Determine if the given operator and value combination is legal.
- *
- * Prevents using Null values with invalid operators.
- *
- * @param string $operator
- * @param mixed $value
- * @return bool
- */
- protected function invalidOperatorAndValue($operator, $value)
- {
- return is_null($value) && in_array($operator, $this->operators) &&
- ! in_array($operator, ['=', '<>', '!=']);
- }
-
- /**
- * Determine if the given operator is supported.
- *
- * @param string $operator
- * @return bool
- */
- protected function invalidOperator($operator)
- {
- return ! in_array(strtolower($operator), $this->operators, true) &&
- ! in_array(strtolower($operator), $this->grammar->getOperators(), true);
- }
-
- /**
- * Add an "or where" clause to the query.
- *
- * @param string|array|\Closure $column
- * @param mixed $operator
- * @param mixed $value
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhere($column, $operator = null, $value = null)
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->where($column, $operator, $value, 'or');
- }
-
- /**
- * Add a "where" clause comparing two columns to the query.
- *
- * @param string|array $first
- * @param string|null $operator
- * @param string|null $second
- * @param string|null $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereColumn($first, $operator = null, $second = null, $boolean = 'and')
- {
- // If the column is an array, we will assume it is an array of key-value pairs
- // and can add them each as a where clause. We will maintain the boolean we
- // received when the method was called and pass it into the nested where.
- if (is_array($first)) {
- return $this->addArrayOfWheres($first, $boolean, 'whereColumn');
- }
-
- // If the given operator is not found in the list of valid operators we will
- // assume that the developer is just short-cutting the '=' operators and
- // we will set the operators to '=' and set the values appropriately.
- if ($this->invalidOperator($operator)) {
- list($second, $operator) = [$operator, '='];
- }
-
- // Finally, we will add this where clause into this array of clauses that we
- // are building for the query. All of them will be compiled via a grammar
- // once the query is about to be executed and run against the database.
- $type = 'Column';
-
- $this->wheres[] = compact(
- 'type', 'first', 'operator', 'second', 'boolean'
- );
-
- return $this;
- }
-
- /**
- * Add an "or where" clause comparing two columns to the query.
- *
- * @param string|array $first
- * @param string|null $operator
- * @param string|null $second
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereColumn($first, $operator = null, $second = null)
- {
- return $this->whereColumn($first, $operator, $second, 'or');
- }
-
- /**
- * Add a raw where clause to the query.
- *
- * @param string $sql
- * @param mixed $bindings
- * @param string $boolean
- * @return $this
- */
- public function whereRaw($sql, $bindings = [], $boolean = 'and')
- {
- $this->wheres[] = ['type' => 'raw', 'sql' => $sql, 'boolean' => $boolean];
-
- $this->addBinding((array) $bindings, 'where');
-
- return $this;
- }
-
- /**
- * Add a raw or where clause to the query.
- *
- * @param string $sql
- * @param mixed $bindings
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereRaw($sql, $bindings = [])
- {
- return $this->whereRaw($sql, $bindings, 'or');
- }
-
- /**
- * Add a "where in" clause to the query.
- *
- * @param string $column
- * @param mixed $values
- * @param string $boolean
- * @param bool $not
- * @return $this
- */
- public function whereIn($column, $values, $boolean = 'and', $not = false)
- {
- $type = $not ? 'NotIn' : 'In';
-
- if ($values instanceof EloquentBuilder) {
- $values = $values->getQuery();
- }
-
- // If the value is a query builder instance we will assume the developer wants to
- // look for any values that exists within this given query. So we will add the
- // query accordingly so that this query is properly executed when it is run.
- if ($values instanceof self) {
- return $this->whereInExistingQuery(
- $column, $values, $boolean, $not
- );
- }
-
- // If the value of the where in clause is actually a Closure, we will assume that
- // the developer is using a full sub-select for this "in" statement, and will
- // execute those Closures, then we can re-construct the entire sub-selects.
- if ($values instanceof Closure) {
- return $this->whereInSub($column, $values, $boolean, $not);
- }
-
- // Next, if the value is Arrayable we need to cast it to its raw array form so we
- // have the underlying array value instead of an Arrayable object which is not
- // able to be added as a binding, etc. We will then add to the wheres array.
- if ($values instanceof Arrayable) {
- $values = $values->toArray();
- }
-
- $this->wheres[] = compact('type', 'column', 'values', 'boolean');
-
- // Finally we'll add a binding for each values unless that value is an expression
- // in which case we will just skip over it since it will be the query as a raw
- // string and not as a parameterized place-holder to be replaced by the PDO.
- foreach ($values as $value) {
- if (! $value instanceof Expression) {
- $this->addBinding($value, 'where');
- }
- }
-
- return $this;
- }
-
- /**
- * Add an "or where in" clause to the query.
- *
- * @param string $column
- * @param mixed $values
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereIn($column, $values)
- {
- return $this->whereIn($column, $values, 'or');
- }
-
- /**
- * Add a "where not in" clause to the query.
- *
- * @param string $column
- * @param mixed $values
- * @param string $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereNotIn($column, $values, $boolean = 'and')
- {
- return $this->whereIn($column, $values, $boolean, true);
- }
-
- /**
- * Add an "or where not in" clause to the query.
- *
- * @param string $column
- * @param mixed $values
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereNotIn($column, $values)
- {
- return $this->whereNotIn($column, $values, 'or');
- }
-
- /**
- * Add a where in with a sub-select to the query.
- *
- * @param string $column
- * @param \Closure $callback
- * @param string $boolean
- * @param bool $not
- * @return $this
- */
- protected function whereInSub($column, Closure $callback, $boolean, $not)
- {
- $type = $not ? 'NotInSub' : 'InSub';
-
- // To create the exists sub-select, we will actually create a query and call the
- // provided callback with the query so the developer may set any of the query
- // conditions they want for the in clause, then we'll put it in this array.
- call_user_func($callback, $query = $this->forSubQuery());
-
- $this->wheres[] = compact('type', 'column', 'query', 'boolean');
-
- $this->addBinding($query->getBindings(), 'where');
-
- return $this;
- }
-
- /**
- * Add an external sub-select to the query.
- *
- * @param string $column
- * @param \Illuminate\Database\Query\Builder|static $query
- * @param string $boolean
- * @param bool $not
- * @return $this
- */
- protected function whereInExistingQuery($column, $query, $boolean, $not)
- {
- $type = $not ? 'NotInSub' : 'InSub';
-
- $this->wheres[] = compact('type', 'column', 'query', 'boolean');
-
- $this->addBinding($query->getBindings(), 'where');
-
- return $this;
- }
-
- /**
- * Add a "where null" clause to the query.
- *
- * @param string $column
- * @param string $boolean
- * @param bool $not
- * @return $this
- */
- public function whereNull($column, $boolean = 'and', $not = false)
- {
- $type = $not ? 'NotNull' : 'Null';
-
- $this->wheres[] = compact('type', 'column', 'boolean');
-
- return $this;
- }
-
- /**
- * Add an "or where null" clause to the query.
- *
- * @param string $column
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereNull($column)
- {
- return $this->whereNull($column, 'or');
- }
-
- /**
- * Add a "where not null" clause to the query.
- *
- * @param string $column
- * @param string $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereNotNull($column, $boolean = 'and')
- {
- return $this->whereNull($column, $boolean, true);
- }
-
- /**
- * Add a where between statement to the query.
- *
- * @param string $column
- * @param array $values
- * @param string $boolean
- * @param bool $not
- * @return $this
- */
- public function whereBetween($column, array $values, $boolean = 'and', $not = false)
- {
- $type = 'between';
-
- $this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not');
-
- $this->addBinding($this->cleanBindings($values), 'where');
-
- return $this;
- }
-
- /**
- * Add an or where between statement to the query.
- *
- * @param string $column
- * @param array $values
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereBetween($column, array $values)
- {
- return $this->whereBetween($column, $values, 'or');
- }
-
- /**
- * Add a where not between statement to the query.
- *
- * @param string $column
- * @param array $values
- * @param string $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereNotBetween($column, array $values, $boolean = 'and')
- {
- return $this->whereBetween($column, $values, $boolean, true);
- }
-
- /**
- * Add an or where not between statement to the query.
- *
- * @param string $column
- * @param array $values
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereNotBetween($column, array $values)
- {
- return $this->whereNotBetween($column, $values, 'or');
- }
-
- /**
- * Add an "or where not null" clause to the query.
- *
- * @param string $column
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereNotNull($column)
- {
- return $this->whereNotNull($column, 'or');
- }
-
- /**
- * Add a "where date" statement to the query.
- *
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @param string $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereDate($column, $operator, $value = null, $boolean = 'and')
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->addDateBasedWhere('Date', $column, $operator, $value, $boolean);
- }
-
- /**
- * Add an "or where date" statement to the query.
- *
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereDate($column, $operator, $value = null)
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->whereDate($column, $operator, $value, 'or');
- }
-
- /**
- * Add a "where time" statement to the query.
- *
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @param string $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereTime($column, $operator, $value = null, $boolean = 'and')
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->addDateBasedWhere('Time', $column, $operator, $value, $boolean);
- }
-
- /**
- * Add an "or where time" statement to the query.
- *
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereTime($column, $operator, $value = null)
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->whereTime($column, $operator, $value, 'or');
- }
-
- /**
- * Add a "where day" statement to the query.
- *
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @param string $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereDay($column, $operator, $value = null, $boolean = 'and')
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->addDateBasedWhere('Day', $column, $operator, $value, $boolean);
- }
-
- /**
- * Add an "or where day" statement to the query.
- *
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereDay($column, $operator, $value = null)
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->addDateBasedWhere('Day', $column, $operator, $value, 'or');
- }
-
- /**
- * Add a "where month" statement to the query.
- *
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @param string $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereMonth($column, $operator, $value = null, $boolean = 'and')
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->addDateBasedWhere('Month', $column, $operator, $value, $boolean);
- }
-
- /**
- * Add an "or where month" statement to the query.
- *
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereMonth($column, $operator, $value = null)
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->addDateBasedWhere('Month', $column, $operator, $value, 'or');
- }
-
- /**
- * Add a "where year" statement to the query.
- *
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @param string $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereYear($column, $operator, $value = null, $boolean = 'and')
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->addDateBasedWhere('Year', $column, $operator, $value, $boolean);
- }
-
- /**
- * Add an "or where year" statement to the query.
- *
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereYear($column, $operator, $value = null)
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->addDateBasedWhere('Year', $column, $operator, $value, 'or');
- }
-
- /**
- * Add a date based (year, month, day, time) statement to the query.
- *
- * @param string $type
- * @param string $column
- * @param string $operator
- * @param mixed $value
- * @param string $boolean
- * @return $this
- */
- protected function addDateBasedWhere($type, $column, $operator, $value, $boolean = 'and')
- {
- $this->wheres[] = compact('column', 'type', 'boolean', 'operator', 'value');
-
- if (! $value instanceof Expression) {
- $this->addBinding($value, 'where');
- }
-
- return $this;
- }
-
- /**
- * Add a nested where statement to the query.
- *
- * @param \Closure $callback
- * @param string $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereNested(Closure $callback, $boolean = 'and')
- {
- call_user_func($callback, $query = $this->forNestedWhere());
-
- return $this->addNestedWhereQuery($query, $boolean);
- }
-
- /**
- * Create a new query instance for nested where condition.
- *
- * @return \Illuminate\Database\Query\Builder
- */
- public function forNestedWhere()
- {
- return $this->newQuery()->from($this->from);
- }
-
- /**
- * Add another query builder as a nested where to the query builder.
- *
- * @param \Illuminate\Database\Query\Builder|static $query
- * @param string $boolean
- * @return $this
- */
- public function addNestedWhereQuery($query, $boolean = 'and')
- {
- if (count($query->wheres)) {
- $type = 'Nested';
-
- $this->wheres[] = compact('type', 'query', 'boolean');
-
- $this->addBinding($query->getRawBindings()['where'], 'where');
- }
-
- return $this;
- }
-
- /**
- * Add a full sub-select to the query.
- *
- * @param string $column
- * @param string $operator
- * @param \Closure $callback
- * @param string $boolean
- * @return $this
- */
- protected function whereSub($column, $operator, Closure $callback, $boolean)
- {
- $type = 'Sub';
-
- // Once we have the query instance we can simply execute it so it can add all
- // of the sub-select's conditions to itself, and then we can cache it off
- // in the array of where clauses for the "main" parent query instance.
- call_user_func($callback, $query = $this->forSubQuery());
-
- $this->wheres[] = compact(
- 'type', 'column', 'operator', 'query', 'boolean'
- );
-
- $this->addBinding($query->getBindings(), 'where');
-
- return $this;
- }
-
- /**
- * Add an exists clause to the query.
- *
- * @param \Closure $callback
- * @param string $boolean
- * @param bool $not
- * @return $this
- */
- public function whereExists(Closure $callback, $boolean = 'and', $not = false)
- {
- $query = $this->forSubQuery();
-
- // Similar to the sub-select clause, we will create a new query instance so
- // the developer may cleanly specify the entire exists query and we will
- // compile the whole thing in the grammar and insert it into the SQL.
- call_user_func($callback, $query);
-
- return $this->addWhereExistsQuery($query, $boolean, $not);
- }
-
- /**
- * Add an or exists clause to the query.
- *
- * @param \Closure $callback
- * @param bool $not
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereExists(Closure $callback, $not = false)
- {
- return $this->whereExists($callback, 'or', $not);
- }
-
- /**
- * Add a where not exists clause to the query.
- *
- * @param \Closure $callback
- * @param string $boolean
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function whereNotExists(Closure $callback, $boolean = 'and')
- {
- return $this->whereExists($callback, $boolean, true);
- }
-
- /**
- * Add a where not exists clause to the query.
- *
- * @param \Closure $callback
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orWhereNotExists(Closure $callback)
- {
- return $this->orWhereExists($callback, true);
- }
-
- /**
- * Add an exists clause to the query.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @param string $boolean
- * @param bool $not
- * @return $this
- */
- public function addWhereExistsQuery(self $query, $boolean = 'and', $not = false)
- {
- $type = $not ? 'NotExists' : 'Exists';
-
- $this->wheres[] = compact('type', 'operator', 'query', 'boolean');
-
- $this->addBinding($query->getBindings(), 'where');
-
- return $this;
- }
-
- /**
- * Adds a where condition using row values.
- *
- * @param array $columns
- * @param string $operator
- * @param array $values
- * @param string $boolean
- * @return $this
- */
- public function whereRowValues($columns, $operator, $values, $boolean = 'and')
- {
- if (count($columns) !== count($values)) {
- throw new InvalidArgumentException('The number of columns must match the number of values');
- }
-
- $type = 'RowValues';
-
- $this->wheres[] = compact('type', 'columns', 'operator', 'values', 'boolean');
-
- $this->addBinding($values);
-
- return $this;
- }
-
- /**
- * Adds a or where condition using row values.
- *
- * @param array $columns
- * @param string $operator
- * @param array $values
- * @return $this
- */
- public function orWhereRowValues($columns, $operator, $values)
- {
- return $this->whereRowValues($columns, $operator, $values, 'or');
- }
-
- /**
- * Add a "where JSON contains" clause to the query.
- *
- * @param string $column
- * @param mixed $value
- * @param string $boolean
- * @param bool $not
- * @return $this
- */
- public function whereJsonContains($column, $value, $boolean = 'and', $not = false)
- {
- $type = 'JsonContains';
-
- $this->wheres[] = compact('type', 'column', 'value', 'boolean', 'not');
-
- if (! $value instanceof Expression) {
- $this->addBinding($this->grammar->prepareBindingForJsonContains($value));
- }
-
- return $this;
- }
-
- /**
- * Add a "or where JSON contains" clause to the query.
- *
- * @param string $column
- * @param mixed $value
- * @return $this
- */
- public function orWhereJsonContains($column, $value)
- {
- return $this->whereJsonContains($column, $value, 'or');
- }
-
- /**
- * Add a "where JSON not contains" clause to the query.
- *
- * @param string $column
- * @param mixed $value
- * @param string $boolean
- * @return $this
- */
- public function whereJsonDoesntContain($column, $value, $boolean = 'and')
- {
- return $this->whereJsonContains($column, $value, $boolean, true);
- }
-
- /**
- * Add a "or where JSON not contains" clause to the query.
- *
- * @param string $column
- * @param mixed $value
- * @return $this
- */
- public function orWhereJsonDoesntContain($column, $value)
- {
- return $this->whereJsonDoesntContain($column, $value, 'or');
- }
-
- /**
- * Handles dynamic "where" clauses to the query.
- *
- * @param string $method
- * @param string $parameters
- * @return $this
- */
- public function dynamicWhere($method, $parameters)
- {
- $finder = substr($method, 5);
-
- $segments = preg_split(
- '/(And|Or)(?=[A-Z])/', $finder, -1, PREG_SPLIT_DELIM_CAPTURE
- );
-
- // The connector variable will determine which connector will be used for the
- // query condition. We will change it as we come across new boolean values
- // in the dynamic method strings, which could contain a number of these.
- $connector = 'and';
-
- $index = 0;
-
- foreach ($segments as $segment) {
- // If the segment is not a boolean connector, we can assume it is a column's name
- // and we will add it to the query as a new constraint as a where clause, then
- // we can keep iterating through the dynamic method string's segments again.
- if ($segment !== 'And' && $segment !== 'Or') {
- $this->addDynamic($segment, $connector, $parameters, $index);
-
- $index++;
- }
-
- // Otherwise, we will store the connector so we know how the next where clause we
- // find in the query should be connected to the previous ones, meaning we will
- // have the proper boolean connector to connect the next where clause found.
- else {
- $connector = $segment;
- }
- }
-
- return $this;
- }
-
- /**
- * Add a single dynamic where clause statement to the query.
- *
- * @param string $segment
- * @param string $connector
- * @param array $parameters
- * @param int $index
- * @return void
- */
- protected function addDynamic($segment, $connector, $parameters, $index)
- {
- // Once we have parsed out the columns and formatted the boolean operators we
- // are ready to add it to this query as a where clause just like any other
- // clause on the query. Then we'll increment the parameter index values.
- $bool = strtolower($connector);
-
- $this->where(Str::snake($segment), '=', $parameters[$index], $bool);
- }
-
- /**
- * Add a "group by" clause to the query.
- *
- * @param array ...$groups
- * @return $this
- */
- public function groupBy(...$groups)
- {
- foreach ($groups as $group) {
- $this->groups = array_merge(
- (array) $this->groups,
- Arr::wrap($group)
- );
- }
-
- return $this;
- }
-
- /**
- * Add a "having" clause to the query.
- *
- * @param string $column
- * @param string|null $operator
- * @param string|null $value
- * @param string $boolean
- * @return $this
- */
- public function having($column, $operator = null, $value = null, $boolean = 'and')
- {
- $type = 'Basic';
-
- // Here we will make some assumptions about the operator. If only 2 values are
- // passed to the method, we will assume that the operator is an equals sign
- // and keep going. Otherwise, we'll require the operator to be passed in.
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- // If the given operator is not found in the list of valid operators we will
- // assume that the developer is just short-cutting the '=' operators and
- // we will set the operators to '=' and set the values appropriately.
- if ($this->invalidOperator($operator)) {
- list($value, $operator) = [$operator, '='];
- }
-
- $this->havings[] = compact('type', 'column', 'operator', 'value', 'boolean');
-
- if (! $value instanceof Expression) {
- $this->addBinding($value, 'having');
- }
-
- return $this;
- }
-
- /**
- * Add a "or having" clause to the query.
- *
- * @param string $column
- * @param string|null $operator
- * @param string|null $value
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orHaving($column, $operator = null, $value = null)
- {
- list($value, $operator) = $this->prepareValueAndOperator(
- $value, $operator, func_num_args() === 2
- );
-
- return $this->having($column, $operator, $value, 'or');
- }
-
- /**
- * Add a raw having clause to the query.
- *
- * @param string $sql
- * @param array $bindings
- * @param string $boolean
- * @return $this
- */
- public function havingRaw($sql, array $bindings = [], $boolean = 'and')
- {
- $type = 'Raw';
-
- $this->havings[] = compact('type', 'sql', 'boolean');
-
- $this->addBinding($bindings, 'having');
-
- return $this;
- }
-
- /**
- * Add a raw or having clause to the query.
- *
- * @param string $sql
- * @param array $bindings
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function orHavingRaw($sql, array $bindings = [])
- {
- return $this->havingRaw($sql, $bindings, 'or');
- }
-
- /**
- * Add an "order by" clause to the query.
- *
- * @param string $column
- * @param string $direction
- * @return $this
- */
- public function orderBy($column, $direction = 'asc')
- {
- $this->{$this->unions ? 'unionOrders' : 'orders'}[] = [
- 'column' => $column,
- 'direction' => strtolower($direction) == 'asc' ? 'asc' : 'desc',
- ];
-
- return $this;
- }
-
- /**
- * Add a descending "order by" clause to the query.
- *
- * @param string $column
- * @return $this
- */
- public function orderByDesc($column)
- {
- return $this->orderBy($column, 'desc');
- }
-
- /**
- * Add an "order by" clause for a timestamp to the query.
- *
- * @param string $column
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function latest($column = 'created_at')
- {
- return $this->orderBy($column, 'desc');
- }
-
- /**
- * Add an "order by" clause for a timestamp to the query.
- *
- * @param string $column
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function oldest($column = 'created_at')
- {
- return $this->orderBy($column, 'asc');
- }
-
- /**
- * Put the query's results in random order.
- *
- * @param string $seed
- * @return $this
- */
- public function inRandomOrder($seed = '')
- {
- return $this->orderByRaw($this->grammar->compileRandom($seed));
- }
-
- /**
- * Add a raw "order by" clause to the query.
- *
- * @param string $sql
- * @param array $bindings
- * @return $this
- */
- public function orderByRaw($sql, $bindings = [])
- {
- $type = 'Raw';
-
- $this->{$this->unions ? 'unionOrders' : 'orders'}[] = compact('type', 'sql');
-
- $this->addBinding($bindings, 'order');
-
- return $this;
- }
-
- /**
- * Alias to set the "offset" value of the query.
- *
- * @param int $value
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function skip($value)
- {
- return $this->offset($value);
- }
-
- /**
- * Set the "offset" value of the query.
- *
- * @param int $value
- * @return $this
- */
- public function offset($value)
- {
- $property = $this->unions ? 'unionOffset' : 'offset';
-
- $this->$property = max(0, $value);
-
- return $this;
- }
-
- /**
- * Alias to set the "limit" value of the query.
- *
- * @param int $value
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function take($value)
- {
- return $this->limit($value);
- }
-
- /**
- * Set the "limit" value of the query.
- *
- * @param int $value
- * @return $this
- */
- public function limit($value)
- {
- $property = $this->unions ? 'unionLimit' : 'limit';
-
- if ($value >= 0) {
- $this->$property = $value;
- }
-
- return $this;
- }
-
- /**
- * Set the limit and offset for a given page.
- *
- * @param int $page
- * @param int $perPage
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function forPage($page, $perPage = 15)
- {
- return $this->skip(($page - 1) * $perPage)->take($perPage);
- }
-
- /**
- * Constrain the query to the next "page" of results after a given ID.
- *
- * @param int $perPage
- * @param int|null $lastId
- * @param string $column
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function forPageAfterId($perPage = 15, $lastId = 0, $column = 'id')
- {
- $this->orders = $this->removeExistingOrdersFor($column);
-
- if (! is_null($lastId)) {
- $this->where($column, '>', $lastId);
- }
-
- return $this->orderBy($column, 'asc')
- ->take($perPage);
- }
-
- /**
- * Get an array with all orders with a given column removed.
- *
- * @param string $column
- * @return array
- */
- protected function removeExistingOrdersFor($column)
- {
- return Collection::make($this->orders)
- ->reject(function ($order) use ($column) {
- return isset($order['column'])
- ? $order['column'] === $column : false;
- })->values()->all();
- }
-
- /**
- * Add a union statement to the query.
- *
- * @param \Illuminate\Database\Query\Builder|\Closure $query
- * @param bool $all
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function union($query, $all = false)
- {
- if ($query instanceof Closure) {
- call_user_func($query, $query = $this->newQuery());
- }
-
- $this->unions[] = compact('query', 'all');
-
- $this->addBinding($query->getBindings(), 'union');
-
- return $this;
- }
-
- /**
- * Add a union all statement to the query.
- *
- * @param \Illuminate\Database\Query\Builder|\Closure $query
- * @return \Illuminate\Database\Query\Builder|static
- */
- public function unionAll($query)
- {
- return $this->union($query, true);
- }
-
- /**
- * Lock the selected rows in the table.
- *
- * @param string|bool $value
- * @return $this
- */
- public function lock($value = true)
- {
- $this->lock = $value;
-
- if (! is_null($this->lock)) {
- $this->useWritePdo();
- }
-
- return $this;
- }
-
- /**
- * Lock the selected rows in the table for updating.
- *
- * @return \Illuminate\Database\Query\Builder
- */
- public function lockForUpdate()
- {
- return $this->lock(true);
- }
-
- /**
- * Share lock the selected rows in the table.
- *
- * @return \Illuminate\Database\Query\Builder
- */
- public function sharedLock()
- {
- return $this->lock(false);
- }
-
- /**
- * Get the SQL representation of the query.
- *
- * @return string
- */
- public function toSql()
- {
- return $this->grammar->compileSelect($this);
- }
-
- /**
- * Execute a query for a single record by ID.
- *
- * @param int $id
- * @param array $columns
- * @return mixed|static
- */
- public function find($id, $columns = ['*'])
- {
- return $this->where('id', '=', $id)->first($columns);
- }
-
- /**
- * Get a single column's value from the first result of a query.
- *
- * @param string $column
- * @return mixed
- */
- public function value($column)
- {
- $result = (array) $this->first([$column]);
-
- return count($result) > 0 ? reset($result) : null;
- }
-
- /**
- * Execute the query as a "select" statement.
- *
- * @param array $columns
- * @return \Illuminate\Support\Collection
- */
- public function get($columns = ['*'])
- {
- return collect($this->onceWithColumns($columns, function () {
- return $this->processor->processSelect($this, $this->runSelect());
- }));
- }
-
- /**
- * Run the query as a "select" statement against the connection.
- *
- * @return array
- */
- protected function runSelect()
- {
- return $this->connection->select(
- $this->toSql(), $this->getBindings(), ! $this->useWritePdo
- );
- }
-
- /**
- * Paginate the given query into a simple paginator.
- *
- * @param int $perPage
- * @param array $columns
- * @param string $pageName
- * @param int|null $page
- * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
- */
- public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
- {
- $page = $page ?: Paginator::resolveCurrentPage($pageName);
-
- $total = $this->getCountForPagination($columns);
-
- $results = $total ? $this->forPage($page, $perPage)->get($columns) : collect();
-
- return $this->paginator($results, $total, $perPage, $page, [
- 'path' => Paginator::resolveCurrentPath(),
- 'pageName' => $pageName,
- ]);
- }
-
- /**
- * Get a paginator only supporting simple next and previous links.
- *
- * This is more efficient on larger data-sets, etc.
- *
- * @param int $perPage
- * @param array $columns
- * @param string $pageName
- * @param int|null $page
- * @return \Illuminate\Contracts\Pagination\Paginator
- */
- public function simplePaginate($perPage = 15, $columns = ['*'], $pageName = 'page', $page = null)
- {
- $page = $page ?: Paginator::resolveCurrentPage($pageName);
-
- $this->skip(($page - 1) * $perPage)->take($perPage + 1);
-
- return $this->simplePaginator($this->get($columns), $perPage, $page, [
- 'path' => Paginator::resolveCurrentPath(),
- 'pageName' => $pageName,
- ]);
- }
-
- /**
- * Get the count of the total records for the paginator.
- *
- * @param array $columns
- * @return int
- */
- public function getCountForPagination($columns = ['*'])
- {
- $results = $this->runPaginationCountQuery($columns);
-
- // Once we have run the pagination count query, we will get the resulting count and
- // take into account what type of query it was. When there is a group by we will
- // just return the count of the entire results set since that will be correct.
- if (isset($this->groups)) {
- return count($results);
- } elseif (! isset($results[0])) {
- return 0;
- } elseif (is_object($results[0])) {
- return (int) $results[0]->aggregate;
- }
-
- return (int) array_change_key_case((array) $results[0])['aggregate'];
- }
-
- /**
- * Run a pagination count query.
- *
- * @param array $columns
- * @return array
- */
- protected function runPaginationCountQuery($columns = ['*'])
- {
- return $this->cloneWithout(['columns', 'orders', 'limit', 'offset'])
- ->cloneWithoutBindings(['select', 'order'])
- ->setAggregate('count', $this->withoutSelectAliases($columns))
- ->get()->all();
- }
-
- /**
- * Remove the column aliases since they will break count queries.
- *
- * @param array $columns
- * @return array
- */
- protected function withoutSelectAliases(array $columns)
- {
- return array_map(function ($column) {
- return is_string($column) && ($aliasPosition = strpos(strtolower($column), ' as ')) !== false
- ? substr($column, 0, $aliasPosition) : $column;
- }, $columns);
- }
-
- /**
- * Get a generator for the given query.
- *
- * @return \Generator
- */
- public function cursor()
- {
- if (is_null($this->columns)) {
- $this->columns = ['*'];
- }
-
- return $this->connection->cursor(
- $this->toSql(), $this->getBindings(), ! $this->useWritePdo
- );
- }
-
- /**
- * Chunk the results of a query by comparing numeric IDs.
- *
- * @param int $count
- * @param callable $callback
- * @param string $column
- * @param string $alias
- * @return bool
- */
- public function chunkById($count, callable $callback, $column = 'id', $alias = null)
- {
- $alias = $alias ?: $column;
-
- $lastId = null;
-
- do {
- $clone = clone $this;
-
- // We'll execute the query for the given page and get the results. If there are
- // no results we can just break and return from here. When there are results
- // we will call the callback with the current chunk of these results here.
- $results = $clone->forPageAfterId($count, $lastId, $column)->get();
-
- $countResults = $results->count();
-
- if ($countResults == 0) {
- break;
- }
-
- // On each chunk result set, we will pass them to the callback and then let the
- // developer take care of everything within the callback, which allows us to
- // keep the memory low for spinning through large result sets for working.
- if ($callback($results) === false) {
- return false;
- }
-
- $lastId = $results->last()->{$alias};
-
- unset($results);
- } while ($countResults == $count);
-
- return true;
- }
-
- /**
- * Throw an exception if the query doesn't have an orderBy clause.
- *
- * @return void
- *
- * @throws \RuntimeException
- */
- protected function enforceOrderBy()
- {
- if (empty($this->orders) && empty($this->unionOrders)) {
- throw new RuntimeException('You must specify an orderBy clause when using this function.');
- }
- }
-
- /**
- * Get an array with the values of a given column.
- *
- * @param string $column
- * @param string|null $key
- * @return \Illuminate\Support\Collection
- */
- public function pluck($column, $key = null)
- {
- // First, we will need to select the results of the query accounting for the
- // given columns / key. Once we have the results, we will be able to take
- // the results and get the exact data that was requested for the query.
- $queryResult = $this->onceWithColumns(
- is_null($key) ? [$column] : [$column, $key],
- function () {
- return $this->processor->processSelect(
- $this, $this->runSelect()
- );
- }
- );
-
- if (empty($queryResult)) {
- return collect();
- }
-
- // If the columns are qualified with a table or have an alias, we cannot use
- // those directly in the "pluck" operations since the results from the DB
- // are only keyed by the column itself. We'll strip the table out here.
- $column = $this->stripTableForPluck($column);
-
- $key = $this->stripTableForPluck($key);
-
- return is_array($queryResult[0])
- ? $this->pluckFromArrayColumn($queryResult, $column, $key)
- : $this->pluckFromObjectColumn($queryResult, $column, $key);
- }
-
- /**
- * Strip off the table name or alias from a column identifier.
- *
- * @param string $column
- * @return string|null
- */
- protected function stripTableForPluck($column)
- {
- return is_null($column) ? $column : last(preg_split('~\.| ~', $column));
- }
-
- /**
- * Retrieve column values from rows represented as objects.
- *
- * @param array $queryResult
- * @param string $column
- * @param string $key
- * @return \Illuminate\Support\Collection
- */
- protected function pluckFromObjectColumn($queryResult, $column, $key)
- {
- $results = [];
-
- if (is_null($key)) {
- foreach ($queryResult as $row) {
- $results[] = $row->$column;
- }
- } else {
- foreach ($queryResult as $row) {
- $results[$row->$key] = $row->$column;
- }
- }
-
- return collect($results);
- }
-
- /**
- * Retrieve column values from rows represented as arrays.
- *
- * @param array $queryResult
- * @param string $column
- * @param string $key
- * @return \Illuminate\Support\Collection
- */
- protected function pluckFromArrayColumn($queryResult, $column, $key)
- {
- $results = [];
-
- if (is_null($key)) {
- foreach ($queryResult as $row) {
- $results[] = $row[$column];
- }
- } else {
- foreach ($queryResult as $row) {
- $results[$row[$key]] = $row[$column];
- }
- }
-
- return collect($results);
- }
-
- /**
- * Concatenate values of a given column as a string.
- *
- * @param string $column
- * @param string $glue
- * @return string
- */
- public function implode($column, $glue = '')
- {
- return $this->pluck($column)->implode($glue);
- }
-
- /**
- * Determine if any rows exist for the current query.
- *
- * @return bool
- */
- public function exists()
- {
- $results = $this->connection->select(
- $this->grammar->compileExists($this), $this->getBindings(), ! $this->useWritePdo
- );
-
- // If the results has rows, we will get the row and see if the exists column is a
- // boolean true. If there is no results for this query we will return false as
- // there are no rows for this query at all and we can return that info here.
- if (isset($results[0])) {
- $results = (array) $results[0];
-
- return (bool) $results['exists'];
- }
-
- return false;
- }
-
- /**
- * Determine if no rows exist for the current query.
- *
- * @return bool
- */
- public function doesntExist()
- {
- return ! $this->exists();
- }
-
- /**
- * Retrieve the "count" result of the query.
- *
- * @param string $columns
- * @return int
- */
- public function count($columns = '*')
- {
- return (int) $this->aggregate(__FUNCTION__, Arr::wrap($columns));
- }
-
- /**
- * Retrieve the minimum value of a given column.
- *
- * @param string $column
- * @return mixed
- */
- public function min($column)
- {
- return $this->aggregate(__FUNCTION__, [$column]);
- }
-
- /**
- * Retrieve the maximum value of a given column.
- *
- * @param string $column
- * @return mixed
- */
- public function max($column)
- {
- return $this->aggregate(__FUNCTION__, [$column]);
- }
-
- /**
- * Retrieve the sum of the values of a given column.
- *
- * @param string $column
- * @return mixed
- */
- public function sum($column)
- {
- $result = $this->aggregate(__FUNCTION__, [$column]);
-
- return $result ?: 0;
- }
-
- /**
- * Retrieve the average of the values of a given column.
- *
- * @param string $column
- * @return mixed
- */
- public function avg($column)
- {
- return $this->aggregate(__FUNCTION__, [$column]);
- }
-
- /**
- * Alias for the "avg" method.
- *
- * @param string $column
- * @return mixed
- */
- public function average($column)
- {
- return $this->avg($column);
- }
-
- /**
- * Execute an aggregate function on the database.
- *
- * @param string $function
- * @param array $columns
- * @return mixed
- */
- public function aggregate($function, $columns = ['*'])
- {
- $results = $this->cloneWithout(['columns'])
- ->cloneWithoutBindings(['select'])
- ->setAggregate($function, $columns)
- ->get($columns);
-
- if (! $results->isEmpty()) {
- return array_change_key_case((array) $results[0])['aggregate'];
- }
- }
-
- /**
- * Execute a numeric aggregate function on the database.
- *
- * @param string $function
- * @param array $columns
- * @return float|int
- */
- public function numericAggregate($function, $columns = ['*'])
- {
- $result = $this->aggregate($function, $columns);
-
- // If there is no result, we can obviously just return 0 here. Next, we will check
- // if the result is an integer or float. If it is already one of these two data
- // types we can just return the result as-is, otherwise we will convert this.
- if (! $result) {
- return 0;
- }
-
- if (is_int($result) || is_float($result)) {
- return $result;
- }
-
- // If the result doesn't contain a decimal place, we will assume it is an int then
- // cast it to one. When it does we will cast it to a float since it needs to be
- // cast to the expected data type for the developers out of pure convenience.
- return strpos((string) $result, '.') === false
- ? (int) $result : (float) $result;
- }
-
- /**
- * Set the aggregate property without running the query.
- *
- * @param string $function
- * @param array $columns
- * @return $this
- */
- protected function setAggregate($function, $columns)
- {
- $this->aggregate = compact('function', 'columns');
-
- if (empty($this->groups)) {
- $this->orders = null;
-
- $this->bindings['order'] = [];
- }
-
- return $this;
- }
-
- /**
- * Execute the given callback while selecting the given columns.
- *
- * After running the callback, the columns are reset to the original value.
- *
- * @param array $columns
- * @param callable $callback
- * @return mixed
- */
- protected function onceWithColumns($columns, $callback)
- {
- $original = $this->columns;
-
- if (is_null($original)) {
- $this->columns = $columns;
- }
-
- $result = $callback();
-
- $this->columns = $original;
-
- return $result;
- }
-
- /**
- * Insert a new record into the database.
- *
- * @param array $values
- * @return bool
- */
- public function insert(array $values)
- {
- // Since every insert gets treated like a batch insert, we will make sure the
- // bindings are structured in a way that is convenient when building these
- // inserts statements by verifying these elements are actually an array.
- if (empty($values)) {
- return true;
- }
-
- if (! is_array(reset($values))) {
- $values = [$values];
- }
-
- // Here, we will sort the insert keys for every record so that each insert is
- // in the same order for the record. We need to make sure this is the case
- // so there are not any errors or problems when inserting these records.
- else {
- foreach ($values as $key => $value) {
- ksort($value);
-
- $values[$key] = $value;
- }
- }
-
- // Finally, we will run this query against the database connection and return
- // the results. We will need to also flatten these bindings before running
- // the query so they are all in one huge, flattened array for execution.
- return $this->connection->insert(
- $this->grammar->compileInsert($this, $values),
- $this->cleanBindings(Arr::flatten($values, 1))
- );
- }
-
- /**
- * Insert a new record and get the value of the primary key.
- *
- * @param array $values
- * @param string|null $sequence
- * @return int
- */
- public function insertGetId(array $values, $sequence = null)
- {
- $sql = $this->grammar->compileInsertGetId($this, $values, $sequence);
-
- $values = $this->cleanBindings($values);
-
- return $this->processor->processInsertGetId($this, $sql, $values, $sequence);
- }
-
- /**
- * Update a record in the database.
- *
- * @param array $values
- * @return int
- */
- public function update(array $values)
- {
- $sql = $this->grammar->compileUpdate($this, $values);
-
- return $this->connection->update($sql, $this->cleanBindings(
- $this->grammar->prepareBindingsForUpdate($this->bindings, $values)
- ));
- }
-
- /**
- * Insert or update a record matching the attributes, and fill it with values.
- *
- * @param array $attributes
- * @param array $values
- * @return bool
- */
- public function updateOrInsert(array $attributes, array $values = [])
- {
- if (! $this->where($attributes)->exists()) {
- return $this->insert(array_merge($attributes, $values));
- }
-
- return (bool) $this->take(1)->update($values);
- }
-
- /**
- * Increment a column's value by a given amount.
- *
- * @param string $column
- * @param float|int $amount
- * @param array $extra
- * @return int
- */
- public function increment($column, $amount = 1, array $extra = [])
- {
- if (! is_numeric($amount)) {
- throw new InvalidArgumentException('Non-numeric value passed to increment method.');
- }
-
- $wrapped = $this->grammar->wrap($column);
-
- $columns = array_merge([$column => $this->raw("$wrapped + $amount")], $extra);
-
- return $this->update($columns);
- }
-
- /**
- * Decrement a column's value by a given amount.
- *
- * @param string $column
- * @param float|int $amount
- * @param array $extra
- * @return int
- */
- public function decrement($column, $amount = 1, array $extra = [])
- {
- if (! is_numeric($amount)) {
- throw new InvalidArgumentException('Non-numeric value passed to decrement method.');
- }
-
- $wrapped = $this->grammar->wrap($column);
-
- $columns = array_merge([$column => $this->raw("$wrapped - $amount")], $extra);
-
- return $this->update($columns);
- }
-
- /**
- * Delete a record from the database.
- *
- * @param mixed $id
- * @return int
- */
- public function delete($id = null)
- {
- // If an ID is passed to the method, we will set the where clause to check the
- // ID to let developers to simply and quickly remove a single row from this
- // database without manually specifying the "where" clauses on the query.
- if (! is_null($id)) {
- $this->where($this->from.'.id', '=', $id);
- }
-
- return $this->connection->delete(
- $this->grammar->compileDelete($this), $this->cleanBindings(
- $this->grammar->prepareBindingsForDelete($this->bindings)
- )
- );
- }
-
- /**
- * Run a truncate statement on the table.
- *
- * @return void
- */
- public function truncate()
- {
- foreach ($this->grammar->compileTruncate($this) as $sql => $bindings) {
- $this->connection->statement($sql, $bindings);
- }
- }
-
- /**
- * Get a new instance of the query builder.
- *
- * @return \Illuminate\Database\Query\Builder
- */
- public function newQuery()
- {
- return new static($this->connection, $this->grammar, $this->processor);
- }
-
- /**
- * Create a new query instance for a sub-query.
- *
- * @return \Illuminate\Database\Query\Builder
- */
- protected function forSubQuery()
- {
- return $this->newQuery();
- }
-
- /**
- * Create a raw database expression.
- *
- * @param mixed $value
- * @return \Illuminate\Database\Query\Expression
- */
- public function raw($value)
- {
- return $this->connection->raw($value);
- }
-
- /**
- * Get the current query value bindings in a flattened array.
- *
- * @return array
- */
- public function getBindings()
- {
- return Arr::flatten($this->bindings);
- }
-
- /**
- * Get the raw array of bindings.
- *
- * @return array
- */
- public function getRawBindings()
- {
- return $this->bindings;
- }
-
- /**
- * Set the bindings on the query builder.
- *
- * @param array $bindings
- * @param string $type
- * @return $this
- *
- * @throws \InvalidArgumentException
- */
- public function setBindings(array $bindings, $type = 'where')
- {
- if (! array_key_exists($type, $this->bindings)) {
- throw new InvalidArgumentException("Invalid binding type: {$type}.");
- }
-
- $this->bindings[$type] = $bindings;
-
- return $this;
- }
-
- /**
- * Add a binding to the query.
- *
- * @param mixed $value
- * @param string $type
- * @return $this
- *
- * @throws \InvalidArgumentException
- */
- public function addBinding($value, $type = 'where')
- {
- if (! array_key_exists($type, $this->bindings)) {
- throw new InvalidArgumentException("Invalid binding type: {$type}.");
- }
-
- if (is_array($value)) {
- $this->bindings[$type] = array_values(array_merge($this->bindings[$type], $value));
- } else {
- $this->bindings[$type][] = $value;
- }
-
- return $this;
- }
-
- /**
- * Merge an array of bindings into our bindings.
- *
- * @param \Illuminate\Database\Query\Builder $query
- * @return $this
- */
- public function mergeBindings(self $query)
- {
- $this->bindings = array_merge_recursive($this->bindings, $query->bindings);
-
- return $this;
- }
-
- /**
- * Remove all of the expressions from a list of bindings.
- *
- * @param array $bindings
- * @return array
- */
- protected function cleanBindings(array $bindings)
- {
- return array_values(array_filter($bindings, function ($binding) {
- return ! $binding instanceof Expression;
- }));
- }
-
- /**
- * Get the database connection instance.
- *
- * @return \Illuminate\Database\ConnectionInterface
- */
- public function getConnection()
- {
- return $this->connection;
- }
-
- /**
- * Get the database query processor instance.
- *
- * @return \Illuminate\Database\Query\Processors\Processor
- */
- public function getProcessor()
- {
- return $this->processor;
- }
-
- /**
- * Get the query grammar instance.
- *
- * @return \Illuminate\Database\Query\Grammars\Grammar
- */
- public function getGrammar()
- {
- return $this->grammar;
- }
-
- /**
- * Use the write pdo for query.
- *
- * @return $this
- */
- public function useWritePdo()
- {
- $this->useWritePdo = true;
-
- return $this;
- }
-
- /**
- * Clone the query without the given properties.
- *
- * @param array $properties
- * @return static
- */
- public function cloneWithout(array $properties)
- {
- return tap(clone $this, function ($clone) use ($properties) {
- foreach ($properties as $property) {
- $clone->{$property} = null;
- }
- });
- }
-
- /**
- * Clone the query without the given bindings.
- *
- * @param array $except
- * @return static
- */
- public function cloneWithoutBindings(array $except)
- {
- return tap(clone $this, function ($clone) use ($except) {
- foreach ($except as $type) {
- $clone->bindings[$type] = [];
- }
- });
- }
-
- /**
- * Handle dynamic method calls into the method.
- *
- * @param string $method
- * @param array $parameters
- * @return mixed
- *
- * @throws \BadMethodCallException
- */
- public function __call($method, $parameters)
- {
- if (static::hasMacro($method)) {
- return $this->macroCall($method, $parameters);
- }
-
- if (Str::startsWith($method, 'where')) {
- return $this->dynamicWhere($method, $parameters);
- }
-
- throw new BadMethodCallException(sprintf(
- 'Method %s::%s does not exist.', static::class, $method
- ));
- }
- }
|