Blueprint.php 35KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. use Closure;
  4. use BadMethodCallException;
  5. use Illuminate\Support\Fluent;
  6. use Illuminate\Database\Connection;
  7. use Illuminate\Support\Traits\Macroable;
  8. use Illuminate\Database\SQLiteConnection;
  9. use Illuminate\Database\Schema\Grammars\Grammar;
  10. class Blueprint
  11. {
  12. use Macroable;
  13. /**
  14. * The table the blueprint describes.
  15. *
  16. * @var string
  17. */
  18. protected $table;
  19. /**
  20. * The columns that should be added to the table.
  21. *
  22. * @var \Illuminate\Support\Fluent[]
  23. */
  24. protected $columns = [];
  25. /**
  26. * The commands that should be run for the table.
  27. *
  28. * @var \Illuminate\Support\Fluent[]
  29. */
  30. protected $commands = [];
  31. /**
  32. * The storage engine that should be used for the table.
  33. *
  34. * @var string
  35. */
  36. public $engine;
  37. /**
  38. * The default character set that should be used for the table.
  39. */
  40. public $charset;
  41. /**
  42. * The collation that should be used for the table.
  43. */
  44. public $collation;
  45. /**
  46. * Whether to make the table temporary.
  47. *
  48. * @var bool
  49. */
  50. public $temporary = false;
  51. /**
  52. * Create a new schema blueprint.
  53. *
  54. * @param string $table
  55. * @param \Closure|null $callback
  56. * @return void
  57. */
  58. public function __construct($table, Closure $callback = null)
  59. {
  60. $this->table = $table;
  61. if (! is_null($callback)) {
  62. $callback($this);
  63. }
  64. }
  65. /**
  66. * Execute the blueprint against the database.
  67. *
  68. * @param \Illuminate\Database\Connection $connection
  69. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  70. * @return void
  71. */
  72. public function build(Connection $connection, Grammar $grammar)
  73. {
  74. foreach ($this->toSql($connection, $grammar) as $statement) {
  75. $connection->statement($statement);
  76. }
  77. }
  78. /**
  79. * Get the raw SQL statements for the blueprint.
  80. *
  81. * @param \Illuminate\Database\Connection $connection
  82. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  83. * @return array
  84. */
  85. public function toSql(Connection $connection, Grammar $grammar)
  86. {
  87. $this->addImpliedCommands($grammar);
  88. $statements = [];
  89. // Each type of command has a corresponding compiler function on the schema
  90. // grammar which is used to build the necessary SQL statements to build
  91. // the blueprint element, so we'll just call that compilers function.
  92. $this->ensureCommandsAreValid($connection);
  93. foreach ($this->commands as $command) {
  94. $method = 'compile'.ucfirst($command->name);
  95. if (method_exists($grammar, $method)) {
  96. if (! is_null($sql = $grammar->$method($this, $command, $connection))) {
  97. $statements = array_merge($statements, (array) $sql);
  98. }
  99. }
  100. }
  101. return $statements;
  102. }
  103. /**
  104. * Ensure the commands on the blueprint are valid for the connection type.
  105. *
  106. * @param \Illuminate\Database\Connection $connection
  107. * @return void
  108. */
  109. protected function ensureCommandsAreValid(Connection $connection)
  110. {
  111. if ($connection instanceof SQLiteConnection &&
  112. $this->commandsNamed(['dropColumn', 'renameColumn'])->count() > 1) {
  113. throw new BadMethodCallException(
  114. "SQLite doesn't support multiple calls to dropColumn / renameColumn in a single modification."
  115. );
  116. }
  117. }
  118. /**
  119. * Get all of the commands matching the given names.
  120. *
  121. * @param array $names
  122. * @return \Illuminate\Support\Collection
  123. */
  124. protected function commandsNamed(array $names)
  125. {
  126. return collect($this->commands)->filter(function ($command) use ($names) {
  127. return in_array($command->name, $names);
  128. });
  129. }
  130. /**
  131. * Add the commands that are implied by the blueprint's state.
  132. *
  133. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  134. * @return void
  135. */
  136. protected function addImpliedCommands(Grammar $grammar)
  137. {
  138. if (count($this->getAddedColumns()) > 0 && ! $this->creating()) {
  139. array_unshift($this->commands, $this->createCommand('add'));
  140. }
  141. if (count($this->getChangedColumns()) > 0 && ! $this->creating()) {
  142. array_unshift($this->commands, $this->createCommand('change'));
  143. }
  144. $this->addFluentIndexes();
  145. $this->addFluentCommands($grammar);
  146. }
  147. /**
  148. * Add the index commands fluently specified on columns.
  149. *
  150. * @return void
  151. */
  152. protected function addFluentIndexes()
  153. {
  154. foreach ($this->columns as $column) {
  155. foreach (['primary', 'unique', 'index', 'spatialIndex'] as $index) {
  156. // If the index has been specified on the given column, but is simply equal
  157. // to "true" (boolean), no name has been specified for this index so the
  158. // index method can be called without a name and it will generate one.
  159. if ($column->{$index} === true) {
  160. $this->{$index}($column->name);
  161. continue 2;
  162. }
  163. // If the index has been specified on the given column, and it has a string
  164. // value, we'll go ahead and call the index method and pass the name for
  165. // the index since the developer specified the explicit name for this.
  166. elseif (isset($column->{$index})) {
  167. $this->{$index}($column->name, $column->{$index});
  168. continue 2;
  169. }
  170. }
  171. }
  172. }
  173. /**
  174. * Add the fluent commands specified on any columns.
  175. *
  176. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  177. * @return void
  178. */
  179. public function addFluentCommands(Grammar $grammar)
  180. {
  181. foreach ($this->columns as $column) {
  182. foreach ($grammar->getFluentCommands() as $commandName) {
  183. $attributeName = lcfirst($commandName);
  184. if (! isset($column->{$attributeName})) {
  185. continue;
  186. }
  187. $value = $column->{$attributeName};
  188. $this->addCommand(
  189. $commandName, compact('value', 'column')
  190. );
  191. }
  192. }
  193. }
  194. /**
  195. * Determine if the blueprint has a create command.
  196. *
  197. * @return bool
  198. */
  199. protected function creating()
  200. {
  201. return collect($this->commands)->contains(function ($command) {
  202. return $command->name == 'create';
  203. });
  204. }
  205. /**
  206. * Indicate that the table needs to be created.
  207. *
  208. * @return \Illuminate\Support\Fluent
  209. */
  210. public function create()
  211. {
  212. return $this->addCommand('create');
  213. }
  214. /**
  215. * Indicate that the table needs to be temporary.
  216. *
  217. * @return void
  218. */
  219. public function temporary()
  220. {
  221. $this->temporary = true;
  222. }
  223. /**
  224. * Indicate that the table should be dropped.
  225. *
  226. * @return \Illuminate\Support\Fluent
  227. */
  228. public function drop()
  229. {
  230. return $this->addCommand('drop');
  231. }
  232. /**
  233. * Indicate that the table should be dropped if it exists.
  234. *
  235. * @return \Illuminate\Support\Fluent
  236. */
  237. public function dropIfExists()
  238. {
  239. return $this->addCommand('dropIfExists');
  240. }
  241. /**
  242. * Indicate that the given columns should be dropped.
  243. *
  244. * @param array|mixed $columns
  245. * @return \Illuminate\Support\Fluent
  246. */
  247. public function dropColumn($columns)
  248. {
  249. $columns = is_array($columns) ? $columns : func_get_args();
  250. return $this->addCommand('dropColumn', compact('columns'));
  251. }
  252. /**
  253. * Indicate that the given columns should be renamed.
  254. *
  255. * @param string $from
  256. * @param string $to
  257. * @return \Illuminate\Support\Fluent
  258. */
  259. public function renameColumn($from, $to)
  260. {
  261. return $this->addCommand('renameColumn', compact('from', 'to'));
  262. }
  263. /**
  264. * Indicate that the given primary key should be dropped.
  265. *
  266. * @param string|array $index
  267. * @return \Illuminate\Support\Fluent
  268. */
  269. public function dropPrimary($index = null)
  270. {
  271. return $this->dropIndexCommand('dropPrimary', 'primary', $index);
  272. }
  273. /**
  274. * Indicate that the given unique key should be dropped.
  275. *
  276. * @param string|array $index
  277. * @return \Illuminate\Support\Fluent
  278. */
  279. public function dropUnique($index)
  280. {
  281. return $this->dropIndexCommand('dropUnique', 'unique', $index);
  282. }
  283. /**
  284. * Indicate that the given index should be dropped.
  285. *
  286. * @param string|array $index
  287. * @return \Illuminate\Support\Fluent
  288. */
  289. public function dropIndex($index)
  290. {
  291. return $this->dropIndexCommand('dropIndex', 'index', $index);
  292. }
  293. /**
  294. * Indicate that the given spatial index should be dropped.
  295. *
  296. * @param string|array $index
  297. * @return \Illuminate\Support\Fluent
  298. */
  299. public function dropSpatialIndex($index)
  300. {
  301. return $this->dropIndexCommand('dropSpatialIndex', 'spatialIndex', $index);
  302. }
  303. /**
  304. * Indicate that the given foreign key should be dropped.
  305. *
  306. * @param string|array $index
  307. * @return \Illuminate\Support\Fluent
  308. */
  309. public function dropForeign($index)
  310. {
  311. return $this->dropIndexCommand('dropForeign', 'foreign', $index);
  312. }
  313. /**
  314. * Indicate that the given indexes should be renamed.
  315. *
  316. * @param string $from
  317. * @param string $to
  318. * @return \Illuminate\Support\Fluent
  319. */
  320. public function renameIndex($from, $to)
  321. {
  322. return $this->addCommand('renameIndex', compact('from', 'to'));
  323. }
  324. /**
  325. * Indicate that the timestamp columns should be dropped.
  326. *
  327. * @return void
  328. */
  329. public function dropTimestamps()
  330. {
  331. $this->dropColumn('created_at', 'updated_at');
  332. }
  333. /**
  334. * Indicate that the timestamp columns should be dropped.
  335. *
  336. * @return void
  337. */
  338. public function dropTimestampsTz()
  339. {
  340. $this->dropTimestamps();
  341. }
  342. /**
  343. * Indicate that the soft delete column should be dropped.
  344. *
  345. * @return void
  346. */
  347. public function dropSoftDeletes()
  348. {
  349. $this->dropColumn('deleted_at');
  350. }
  351. /**
  352. * Indicate that the soft delete column should be dropped.
  353. *
  354. * @return void
  355. */
  356. public function dropSoftDeletesTz()
  357. {
  358. $this->dropSoftDeletes();
  359. }
  360. /**
  361. * Indicate that the remember token column should be dropped.
  362. *
  363. * @return void
  364. */
  365. public function dropRememberToken()
  366. {
  367. $this->dropColumn('remember_token');
  368. }
  369. /**
  370. * Indicate that the polymorphic columns should be dropped.
  371. *
  372. * @param string $name
  373. * @param string|null $indexName
  374. * @return void
  375. */
  376. public function dropMorphs($name, $indexName = null)
  377. {
  378. $this->dropIndex($indexName ?: $this->createIndexName('index', ["{$name}_type", "{$name}_id"]));
  379. $this->dropColumn("{$name}_type", "{$name}_id");
  380. }
  381. /**
  382. * Rename the table to a given name.
  383. *
  384. * @param string $to
  385. * @return \Illuminate\Support\Fluent
  386. */
  387. public function rename($to)
  388. {
  389. return $this->addCommand('rename', compact('to'));
  390. }
  391. /**
  392. * Specify the primary key(s) for the table.
  393. *
  394. * @param string|array $columns
  395. * @param string $name
  396. * @param string|null $algorithm
  397. * @return \Illuminate\Support\Fluent
  398. */
  399. public function primary($columns, $name = null, $algorithm = null)
  400. {
  401. return $this->indexCommand('primary', $columns, $name, $algorithm);
  402. }
  403. /**
  404. * Specify a unique index for the table.
  405. *
  406. * @param string|array $columns
  407. * @param string $name
  408. * @param string|null $algorithm
  409. * @return \Illuminate\Support\Fluent
  410. */
  411. public function unique($columns, $name = null, $algorithm = null)
  412. {
  413. return $this->indexCommand('unique', $columns, $name, $algorithm);
  414. }
  415. /**
  416. * Specify an index for the table.
  417. *
  418. * @param string|array $columns
  419. * @param string $name
  420. * @param string|null $algorithm
  421. * @return \Illuminate\Support\Fluent
  422. */
  423. public function index($columns, $name = null, $algorithm = null)
  424. {
  425. return $this->indexCommand('index', $columns, $name, $algorithm);
  426. }
  427. /**
  428. * Specify a spatial index for the table.
  429. *
  430. * @param string|array $columns
  431. * @param string $name
  432. * @return \Illuminate\Support\Fluent
  433. */
  434. public function spatialIndex($columns, $name = null)
  435. {
  436. return $this->indexCommand('spatialIndex', $columns, $name);
  437. }
  438. /**
  439. * Specify a foreign key for the table.
  440. *
  441. * @param string|array $columns
  442. * @param string $name
  443. * @return \Illuminate\Support\Fluent
  444. */
  445. public function foreign($columns, $name = null)
  446. {
  447. return $this->indexCommand('foreign', $columns, $name);
  448. }
  449. /**
  450. * Create a new auto-incrementing integer (4-byte) column on the table.
  451. *
  452. * @param string $column
  453. * @return \Illuminate\Support\Fluent
  454. */
  455. public function increments($column)
  456. {
  457. return $this->unsignedInteger($column, true);
  458. }
  459. /**
  460. * Create a new auto-incrementing tiny integer (1-byte) column on the table.
  461. *
  462. * @param string $column
  463. * @return \Illuminate\Support\Fluent
  464. */
  465. public function tinyIncrements($column)
  466. {
  467. return $this->unsignedTinyInteger($column, true);
  468. }
  469. /**
  470. * Create a new auto-incrementing small integer (2-byte) column on the table.
  471. *
  472. * @param string $column
  473. * @return \Illuminate\Support\Fluent
  474. */
  475. public function smallIncrements($column)
  476. {
  477. return $this->unsignedSmallInteger($column, true);
  478. }
  479. /**
  480. * Create a new auto-incrementing medium integer (3-byte) column on the table.
  481. *
  482. * @param string $column
  483. * @return \Illuminate\Support\Fluent
  484. */
  485. public function mediumIncrements($column)
  486. {
  487. return $this->unsignedMediumInteger($column, true);
  488. }
  489. /**
  490. * Create a new auto-incrementing big integer (8-byte) column on the table.
  491. *
  492. * @param string $column
  493. * @return \Illuminate\Support\Fluent
  494. */
  495. public function bigIncrements($column)
  496. {
  497. return $this->unsignedBigInteger($column, true);
  498. }
  499. /**
  500. * Create a new char column on the table.
  501. *
  502. * @param string $column
  503. * @param int $length
  504. * @return \Illuminate\Support\Fluent
  505. */
  506. public function char($column, $length = null)
  507. {
  508. $length = $length ?: Builder::$defaultStringLength;
  509. return $this->addColumn('char', $column, compact('length'));
  510. }
  511. /**
  512. * Create a new string column on the table.
  513. *
  514. * @param string $column
  515. * @param int $length
  516. * @return \Illuminate\Support\Fluent
  517. */
  518. public function string($column, $length = null)
  519. {
  520. $length = $length ?: Builder::$defaultStringLength;
  521. return $this->addColumn('string', $column, compact('length'));
  522. }
  523. /**
  524. * Create a new text column on the table.
  525. *
  526. * @param string $column
  527. * @return \Illuminate\Support\Fluent
  528. */
  529. public function text($column)
  530. {
  531. return $this->addColumn('text', $column);
  532. }
  533. /**
  534. * Create a new medium text column on the table.
  535. *
  536. * @param string $column
  537. * @return \Illuminate\Support\Fluent
  538. */
  539. public function mediumText($column)
  540. {
  541. return $this->addColumn('mediumText', $column);
  542. }
  543. /**
  544. * Create a new long text column on the table.
  545. *
  546. * @param string $column
  547. * @return \Illuminate\Support\Fluent
  548. */
  549. public function longText($column)
  550. {
  551. return $this->addColumn('longText', $column);
  552. }
  553. /**
  554. * Create a new integer (4-byte) column on the table.
  555. *
  556. * @param string $column
  557. * @param bool $autoIncrement
  558. * @param bool $unsigned
  559. * @return \Illuminate\Support\Fluent
  560. */
  561. public function integer($column, $autoIncrement = false, $unsigned = false)
  562. {
  563. return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned'));
  564. }
  565. /**
  566. * Create a new tiny integer (1-byte) column on the table.
  567. *
  568. * @param string $column
  569. * @param bool $autoIncrement
  570. * @param bool $unsigned
  571. * @return \Illuminate\Support\Fluent
  572. */
  573. public function tinyInteger($column, $autoIncrement = false, $unsigned = false)
  574. {
  575. return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned'));
  576. }
  577. /**
  578. * Create a new small integer (2-byte) column on the table.
  579. *
  580. * @param string $column
  581. * @param bool $autoIncrement
  582. * @param bool $unsigned
  583. * @return \Illuminate\Support\Fluent
  584. */
  585. public function smallInteger($column, $autoIncrement = false, $unsigned = false)
  586. {
  587. return $this->addColumn('smallInteger', $column, compact('autoIncrement', 'unsigned'));
  588. }
  589. /**
  590. * Create a new medium integer (3-byte) column on the table.
  591. *
  592. * @param string $column
  593. * @param bool $autoIncrement
  594. * @param bool $unsigned
  595. * @return \Illuminate\Support\Fluent
  596. */
  597. public function mediumInteger($column, $autoIncrement = false, $unsigned = false)
  598. {
  599. return $this->addColumn('mediumInteger', $column, compact('autoIncrement', 'unsigned'));
  600. }
  601. /**
  602. * Create a new big integer (8-byte) column on the table.
  603. *
  604. * @param string $column
  605. * @param bool $autoIncrement
  606. * @param bool $unsigned
  607. * @return \Illuminate\Support\Fluent
  608. */
  609. public function bigInteger($column, $autoIncrement = false, $unsigned = false)
  610. {
  611. return $this->addColumn('bigInteger', $column, compact('autoIncrement', 'unsigned'));
  612. }
  613. /**
  614. * Create a new unsigned integer (4-byte) column on the table.
  615. *
  616. * @param string $column
  617. * @param bool $autoIncrement
  618. * @return \Illuminate\Support\Fluent
  619. */
  620. public function unsignedInteger($column, $autoIncrement = false)
  621. {
  622. return $this->integer($column, $autoIncrement, true);
  623. }
  624. /**
  625. * Create a new unsigned tiny integer (1-byte) column on the table.
  626. *
  627. * @param string $column
  628. * @param bool $autoIncrement
  629. * @return \Illuminate\Support\Fluent
  630. */
  631. public function unsignedTinyInteger($column, $autoIncrement = false)
  632. {
  633. return $this->tinyInteger($column, $autoIncrement, true);
  634. }
  635. /**
  636. * Create a new unsigned small integer (2-byte) column on the table.
  637. *
  638. * @param string $column
  639. * @param bool $autoIncrement
  640. * @return \Illuminate\Support\Fluent
  641. */
  642. public function unsignedSmallInteger($column, $autoIncrement = false)
  643. {
  644. return $this->smallInteger($column, $autoIncrement, true);
  645. }
  646. /**
  647. * Create a new unsigned medium integer (3-byte) column on the table.
  648. *
  649. * @param string $column
  650. * @param bool $autoIncrement
  651. * @return \Illuminate\Support\Fluent
  652. */
  653. public function unsignedMediumInteger($column, $autoIncrement = false)
  654. {
  655. return $this->mediumInteger($column, $autoIncrement, true);
  656. }
  657. /**
  658. * Create a new unsigned big integer (8-byte) column on the table.
  659. *
  660. * @param string $column
  661. * @param bool $autoIncrement
  662. * @return \Illuminate\Support\Fluent
  663. */
  664. public function unsignedBigInteger($column, $autoIncrement = false)
  665. {
  666. return $this->bigInteger($column, $autoIncrement, true);
  667. }
  668. /**
  669. * Create a new float column on the table.
  670. *
  671. * @param string $column
  672. * @param int $total
  673. * @param int $places
  674. * @return \Illuminate\Support\Fluent
  675. */
  676. public function float($column, $total = 8, $places = 2)
  677. {
  678. return $this->addColumn('float', $column, compact('total', 'places'));
  679. }
  680. /**
  681. * Create a new double column on the table.
  682. *
  683. * @param string $column
  684. * @param int|null $total
  685. * @param int|null $places
  686. * @return \Illuminate\Support\Fluent
  687. */
  688. public function double($column, $total = null, $places = null)
  689. {
  690. return $this->addColumn('double', $column, compact('total', 'places'));
  691. }
  692. /**
  693. * Create a new decimal column on the table.
  694. *
  695. * @param string $column
  696. * @param int $total
  697. * @param int $places
  698. * @return \Illuminate\Support\Fluent
  699. */
  700. public function decimal($column, $total = 8, $places = 2)
  701. {
  702. return $this->addColumn('decimal', $column, compact('total', 'places'));
  703. }
  704. /**
  705. * Create a new unsigned decimal column on the table.
  706. *
  707. * @param string $column
  708. * @param int $total
  709. * @param int $places
  710. * @return \Illuminate\Support\Fluent
  711. */
  712. public function unsignedDecimal($column, $total = 8, $places = 2)
  713. {
  714. return $this->addColumn('decimal', $column, [
  715. 'total' => $total, 'places' => $places, 'unsigned' => true,
  716. ]);
  717. }
  718. /**
  719. * Create a new boolean column on the table.
  720. *
  721. * @param string $column
  722. * @return \Illuminate\Support\Fluent
  723. */
  724. public function boolean($column)
  725. {
  726. return $this->addColumn('boolean', $column);
  727. }
  728. /**
  729. * Create a new enum column on the table.
  730. *
  731. * @param string $column
  732. * @param array $allowed
  733. * @return \Illuminate\Support\Fluent
  734. */
  735. public function enum($column, array $allowed)
  736. {
  737. return $this->addColumn('enum', $column, compact('allowed'));
  738. }
  739. /**
  740. * Create a new json column on the table.
  741. *
  742. * @param string $column
  743. * @return \Illuminate\Support\Fluent
  744. */
  745. public function json($column)
  746. {
  747. return $this->addColumn('json', $column);
  748. }
  749. /**
  750. * Create a new jsonb column on the table.
  751. *
  752. * @param string $column
  753. * @return \Illuminate\Support\Fluent
  754. */
  755. public function jsonb($column)
  756. {
  757. return $this->addColumn('jsonb', $column);
  758. }
  759. /**
  760. * Create a new date column on the table.
  761. *
  762. * @param string $column
  763. * @return \Illuminate\Support\Fluent
  764. */
  765. public function date($column)
  766. {
  767. return $this->addColumn('date', $column);
  768. }
  769. /**
  770. * Create a new date-time column on the table.
  771. *
  772. * @param string $column
  773. * @param int $precision
  774. * @return \Illuminate\Support\Fluent
  775. */
  776. public function dateTime($column, $precision = 0)
  777. {
  778. return $this->addColumn('dateTime', $column, compact('precision'));
  779. }
  780. /**
  781. * Create a new date-time column (with time zone) on the table.
  782. *
  783. * @param string $column
  784. * @param int $precision
  785. * @return \Illuminate\Support\Fluent
  786. */
  787. public function dateTimeTz($column, $precision = 0)
  788. {
  789. return $this->addColumn('dateTimeTz', $column, compact('precision'));
  790. }
  791. /**
  792. * Create a new time column on the table.
  793. *
  794. * @param string $column
  795. * @param int $precision
  796. * @return \Illuminate\Support\Fluent
  797. */
  798. public function time($column, $precision = 0)
  799. {
  800. return $this->addColumn('time', $column, compact('precision'));
  801. }
  802. /**
  803. * Create a new time column (with time zone) on the table.
  804. *
  805. * @param string $column
  806. * @param int $precision
  807. * @return \Illuminate\Support\Fluent
  808. */
  809. public function timeTz($column, $precision = 0)
  810. {
  811. return $this->addColumn('timeTz', $column, compact('precision'));
  812. }
  813. /**
  814. * Create a new timestamp column on the table.
  815. *
  816. * @param string $column
  817. * @param int $precision
  818. * @return \Illuminate\Support\Fluent
  819. */
  820. public function timestamp($column, $precision = 0)
  821. {
  822. return $this->addColumn('timestamp', $column, compact('precision'));
  823. }
  824. /**
  825. * Create a new timestamp (with time zone) column on the table.
  826. *
  827. * @param string $column
  828. * @param int $precision
  829. * @return \Illuminate\Support\Fluent
  830. */
  831. public function timestampTz($column, $precision = 0)
  832. {
  833. return $this->addColumn('timestampTz', $column, compact('precision'));
  834. }
  835. /**
  836. * Add nullable creation and update timestamps to the table.
  837. *
  838. * @param int $precision
  839. * @return void
  840. */
  841. public function timestamps($precision = 0)
  842. {
  843. $this->timestamp('created_at', $precision)->nullable();
  844. $this->timestamp('updated_at', $precision)->nullable();
  845. }
  846. /**
  847. * Add nullable creation and update timestamps to the table.
  848. *
  849. * Alias for self::timestamps().
  850. *
  851. * @param int $precision
  852. * @return void
  853. */
  854. public function nullableTimestamps($precision = 0)
  855. {
  856. $this->timestamps($precision);
  857. }
  858. /**
  859. * Add creation and update timestampTz columns to the table.
  860. *
  861. * @param int $precision
  862. * @return void
  863. */
  864. public function timestampsTz($precision = 0)
  865. {
  866. $this->timestampTz('created_at', $precision)->nullable();
  867. $this->timestampTz('updated_at', $precision)->nullable();
  868. }
  869. /**
  870. * Add a "deleted at" timestamp for the table.
  871. *
  872. * @param string $column
  873. * @param int $precision
  874. * @return \Illuminate\Support\Fluent
  875. */
  876. public function softDeletes($column = 'deleted_at', $precision = 0)
  877. {
  878. return $this->timestamp($column, $precision)->nullable();
  879. }
  880. /**
  881. * Add a "deleted at" timestampTz for the table.
  882. *
  883. * @param int $precision
  884. * @return \Illuminate\Support\Fluent
  885. */
  886. public function softDeletesTz($precision = 0)
  887. {
  888. return $this->timestampTz('deleted_at', $precision)->nullable();
  889. }
  890. /**
  891. * Create a new year column on the table.
  892. *
  893. * @param string $column
  894. * @return \Illuminate\Support\Fluent
  895. */
  896. public function year($column)
  897. {
  898. return $this->addColumn('year', $column);
  899. }
  900. /**
  901. * Create a new binary column on the table.
  902. *
  903. * @param string $column
  904. * @return \Illuminate\Support\Fluent
  905. */
  906. public function binary($column)
  907. {
  908. return $this->addColumn('binary', $column);
  909. }
  910. /**
  911. * Create a new uuid column on the table.
  912. *
  913. * @param string $column
  914. * @return \Illuminate\Support\Fluent
  915. */
  916. public function uuid($column)
  917. {
  918. return $this->addColumn('uuid', $column);
  919. }
  920. /**
  921. * Create a new IP address column on the table.
  922. *
  923. * @param string $column
  924. * @return \Illuminate\Support\Fluent
  925. */
  926. public function ipAddress($column)
  927. {
  928. return $this->addColumn('ipAddress', $column);
  929. }
  930. /**
  931. * Create a new MAC address column on the table.
  932. *
  933. * @param string $column
  934. * @return \Illuminate\Support\Fluent
  935. */
  936. public function macAddress($column)
  937. {
  938. return $this->addColumn('macAddress', $column);
  939. }
  940. /**
  941. * Create a new geometry column on the table.
  942. *
  943. * @param string $column
  944. * @return \Illuminate\Support\Fluent
  945. */
  946. public function geometry($column)
  947. {
  948. return $this->addColumn('geometry', $column);
  949. }
  950. /**
  951. * Create a new point column on the table.
  952. *
  953. * @param string $column
  954. * @param null|int $srid
  955. * @return \Illuminate\Support\Fluent
  956. */
  957. public function point($column, $srid = null)
  958. {
  959. return $this->addColumn('point', $column, compact('srid'));
  960. }
  961. /**
  962. * Create a new linestring column on the table.
  963. *
  964. * @param string $column
  965. * @return \Illuminate\Support\Fluent
  966. */
  967. public function lineString($column)
  968. {
  969. return $this->addColumn('linestring', $column);
  970. }
  971. /**
  972. * Create a new polygon column on the table.
  973. *
  974. * @param string $column
  975. * @return \Illuminate\Support\Fluent
  976. */
  977. public function polygon($column)
  978. {
  979. return $this->addColumn('polygon', $column);
  980. }
  981. /**
  982. * Create a new geometrycollection column on the table.
  983. *
  984. * @param string $column
  985. * @return \Illuminate\Support\Fluent
  986. */
  987. public function geometryCollection($column)
  988. {
  989. return $this->addColumn('geometrycollection', $column);
  990. }
  991. /**
  992. * Create a new multipoint column on the table.
  993. *
  994. * @param string $column
  995. * @return \Illuminate\Support\Fluent
  996. */
  997. public function multiPoint($column)
  998. {
  999. return $this->addColumn('multipoint', $column);
  1000. }
  1001. /**
  1002. * Create a new multilinestring column on the table.
  1003. *
  1004. * @param string $column
  1005. * @return \Illuminate\Support\Fluent
  1006. */
  1007. public function multiLineString($column)
  1008. {
  1009. return $this->addColumn('multilinestring', $column);
  1010. }
  1011. /**
  1012. * Create a new multipolygon column on the table.
  1013. *
  1014. * @param string $column
  1015. * @return \Illuminate\Support\Fluent
  1016. */
  1017. public function multiPolygon($column)
  1018. {
  1019. return $this->addColumn('multipolygon', $column);
  1020. }
  1021. /**
  1022. * Add the proper columns for a polymorphic table.
  1023. *
  1024. * @param string $name
  1025. * @param string|null $indexName
  1026. * @return void
  1027. */
  1028. public function morphs($name, $indexName = null)
  1029. {
  1030. $this->string("{$name}_type");
  1031. $this->unsignedBigInteger("{$name}_id");
  1032. $this->index(["{$name}_type", "{$name}_id"], $indexName);
  1033. }
  1034. /**
  1035. * Add nullable columns for a polymorphic table.
  1036. *
  1037. * @param string $name
  1038. * @param string|null $indexName
  1039. * @return void
  1040. */
  1041. public function nullableMorphs($name, $indexName = null)
  1042. {
  1043. $this->string("{$name}_type")->nullable();
  1044. $this->unsignedBigInteger("{$name}_id")->nullable();
  1045. $this->index(["{$name}_type", "{$name}_id"], $indexName);
  1046. }
  1047. /**
  1048. * Adds the `remember_token` column to the table.
  1049. *
  1050. * @return \Illuminate\Support\Fluent
  1051. */
  1052. public function rememberToken()
  1053. {
  1054. return $this->string('remember_token', 100)->nullable();
  1055. }
  1056. /**
  1057. * Add a new index command to the blueprint.
  1058. *
  1059. * @param string $type
  1060. * @param string|array $columns
  1061. * @param string $index
  1062. * @param string|null $algorithm
  1063. * @return \Illuminate\Support\Fluent
  1064. */
  1065. protected function indexCommand($type, $columns, $index, $algorithm = null)
  1066. {
  1067. $columns = (array) $columns;
  1068. // If no name was specified for this index, we will create one using a basic
  1069. // convention of the table name, followed by the columns, followed by an
  1070. // index type, such as primary or index, which makes the index unique.
  1071. $index = $index ?: $this->createIndexName($type, $columns);
  1072. return $this->addCommand(
  1073. $type, compact('index', 'columns', 'algorithm')
  1074. );
  1075. }
  1076. /**
  1077. * Create a new drop index command on the blueprint.
  1078. *
  1079. * @param string $command
  1080. * @param string $type
  1081. * @param string|array $index
  1082. * @return \Illuminate\Support\Fluent
  1083. */
  1084. protected function dropIndexCommand($command, $type, $index)
  1085. {
  1086. $columns = [];
  1087. // If the given "index" is actually an array of columns, the developer means
  1088. // to drop an index merely by specifying the columns involved without the
  1089. // conventional name, so we will build the index name from the columns.
  1090. if (is_array($index)) {
  1091. $index = $this->createIndexName($type, $columns = $index);
  1092. }
  1093. return $this->indexCommand($command, $columns, $index);
  1094. }
  1095. /**
  1096. * Create a default index name for the table.
  1097. *
  1098. * @param string $type
  1099. * @param array $columns
  1100. * @return string
  1101. */
  1102. protected function createIndexName($type, array $columns)
  1103. {
  1104. $index = strtolower($this->table.'_'.implode('_', $columns).'_'.$type);
  1105. return str_replace(['-', '.'], '_', $index);
  1106. }
  1107. /**
  1108. * Add a new column to the blueprint.
  1109. *
  1110. * @param string $type
  1111. * @param string $name
  1112. * @param array $parameters
  1113. * @return \Illuminate\Support\Fluent
  1114. */
  1115. public function addColumn($type, $name, array $parameters = [])
  1116. {
  1117. $this->columns[] = $column = new Fluent(
  1118. array_merge(compact('type', 'name'), $parameters)
  1119. );
  1120. return $column;
  1121. }
  1122. /**
  1123. * Remove a column from the schema blueprint.
  1124. *
  1125. * @param string $name
  1126. * @return $this
  1127. */
  1128. public function removeColumn($name)
  1129. {
  1130. $this->columns = array_values(array_filter($this->columns, function ($c) use ($name) {
  1131. return $c['attributes']['name'] != $name;
  1132. }));
  1133. return $this;
  1134. }
  1135. /**
  1136. * Add a new command to the blueprint.
  1137. *
  1138. * @param string $name
  1139. * @param array $parameters
  1140. * @return \Illuminate\Support\Fluent
  1141. */
  1142. protected function addCommand($name, array $parameters = [])
  1143. {
  1144. $this->commands[] = $command = $this->createCommand($name, $parameters);
  1145. return $command;
  1146. }
  1147. /**
  1148. * Create a new Fluent command.
  1149. *
  1150. * @param string $name
  1151. * @param array $parameters
  1152. * @return \Illuminate\Support\Fluent
  1153. */
  1154. protected function createCommand($name, array $parameters = [])
  1155. {
  1156. return new Fluent(array_merge(compact('name'), $parameters));
  1157. }
  1158. /**
  1159. * Get the table the blueprint describes.
  1160. *
  1161. * @return string
  1162. */
  1163. public function getTable()
  1164. {
  1165. return $this->table;
  1166. }
  1167. /**
  1168. * Get the columns on the blueprint.
  1169. *
  1170. * @return \Illuminate\Support\Fluent[]
  1171. */
  1172. public function getColumns()
  1173. {
  1174. return $this->columns;
  1175. }
  1176. /**
  1177. * Get the commands on the blueprint.
  1178. *
  1179. * @return \Illuminate\Support\Fluent[]
  1180. */
  1181. public function getCommands()
  1182. {
  1183. return $this->commands;
  1184. }
  1185. /**
  1186. * Get the columns on the blueprint that should be added.
  1187. *
  1188. * @return \Illuminate\Support\Fluent[]
  1189. */
  1190. public function getAddedColumns()
  1191. {
  1192. return array_filter($this->columns, function ($column) {
  1193. return ! $column->change;
  1194. });
  1195. }
  1196. /**
  1197. * Get the columns on the blueprint that should be changed.
  1198. *
  1199. * @return \Illuminate\Support\Fluent[]
  1200. */
  1201. public function getChangedColumns()
  1202. {
  1203. return array_filter($this->columns, function ($column) {
  1204. return (bool) $column->change;
  1205. });
  1206. }
  1207. }