PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php

https://gitlab.com/ealexis.t/trends
PHP | 1062 lines | 398 code | 115 blank | 549 comment | 13 complexity | 12c9c919c91602a005366491caf98c87 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. use Closure;
  4. use Illuminate\Support\Fluent;
  5. use Illuminate\Database\Connection;
  6. use Illuminate\Database\Schema\Grammars\Grammar;
  7. class Blueprint
  8. {
  9. /**
  10. * The table the blueprint describes.
  11. *
  12. * @var string
  13. */
  14. protected $table;
  15. /**
  16. * The columns that should be added to the table.
  17. *
  18. * @var array
  19. */
  20. protected $columns = [];
  21. /**
  22. * The commands that should be run for the table.
  23. *
  24. * @var array
  25. */
  26. protected $commands = [];
  27. /**
  28. * The storage engine that should be used for the table.
  29. *
  30. * @var string
  31. */
  32. public $engine;
  33. /**
  34. * The default character set that should be used for the table.
  35. */
  36. public $charset;
  37. /**
  38. * The collation that should be used for the table.
  39. */
  40. public $collation;
  41. /**
  42. * Whether to make the table temporary.
  43. *
  44. * @var bool
  45. */
  46. public $temporary = false;
  47. /**
  48. * Create a new schema blueprint.
  49. *
  50. * @param string $table
  51. * @param \Closure|null $callback
  52. * @return void
  53. */
  54. public function __construct($table, Closure $callback = null)
  55. {
  56. $this->table = $table;
  57. if (! is_null($callback)) {
  58. $callback($this);
  59. }
  60. }
  61. /**
  62. * Execute the blueprint against the database.
  63. *
  64. * @param \Illuminate\Database\Connection $connection
  65. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  66. * @return void
  67. */
  68. public function build(Connection $connection, Grammar $grammar)
  69. {
  70. foreach ($this->toSql($connection, $grammar) as $statement) {
  71. $connection->statement($statement);
  72. }
  73. }
  74. /**
  75. * Get the raw SQL statements for the blueprint.
  76. *
  77. * @param \Illuminate\Database\Connection $connection
  78. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  79. * @return array
  80. */
  81. public function toSql(Connection $connection, Grammar $grammar)
  82. {
  83. $this->addImpliedCommands();
  84. $statements = [];
  85. // Each type of command has a corresponding compiler function on the schema
  86. // grammar which is used to build the necessary SQL statements to build
  87. // the blueprint element, so we'll just call that compilers function.
  88. foreach ($this->commands as $command) {
  89. $method = 'compile'.ucfirst($command->name);
  90. if (method_exists($grammar, $method)) {
  91. if (! is_null($sql = $grammar->$method($this, $command, $connection))) {
  92. $statements = array_merge($statements, (array) $sql);
  93. }
  94. }
  95. }
  96. return $statements;
  97. }
  98. /**
  99. * Add the commands that are implied by the blueprint.
  100. *
  101. * @return void
  102. */
  103. protected function addImpliedCommands()
  104. {
  105. if (count($this->getAddedColumns()) > 0 && ! $this->creating()) {
  106. array_unshift($this->commands, $this->createCommand('add'));
  107. }
  108. if (count($this->getChangedColumns()) > 0 && ! $this->creating()) {
  109. array_unshift($this->commands, $this->createCommand('change'));
  110. }
  111. $this->addFluentIndexes();
  112. }
  113. /**
  114. * Add the index commands fluently specified on columns.
  115. *
  116. * @return void
  117. */
  118. protected function addFluentIndexes()
  119. {
  120. foreach ($this->columns as $column) {
  121. foreach (['primary', 'unique', 'index'] as $index) {
  122. // If the index has been specified on the given column, but is simply
  123. // equal to "true" (boolean), no name has been specified for this
  124. // index, so we will simply call the index methods without one.
  125. if ($column->$index === true) {
  126. $this->$index($column->name);
  127. continue 2;
  128. }
  129. // If the index has been specified on the column and it is something
  130. // other than boolean true, we will assume a name was provided on
  131. // the index specification, and pass in the name to the method.
  132. elseif (isset($column->$index)) {
  133. $this->$index($column->name, $column->$index);
  134. continue 2;
  135. }
  136. }
  137. }
  138. }
  139. /**
  140. * Determine if the blueprint has a create command.
  141. *
  142. * @return bool
  143. */
  144. protected function creating()
  145. {
  146. foreach ($this->commands as $command) {
  147. if ($command->name == 'create') {
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. /**
  154. * Indicate that the table needs to be created.
  155. *
  156. * @return \Illuminate\Support\Fluent
  157. */
  158. public function create()
  159. {
  160. return $this->addCommand('create');
  161. }
  162. /**
  163. * Indicate that the table needs to be temporary.
  164. *
  165. * @return void
  166. */
  167. public function temporary()
  168. {
  169. $this->temporary = true;
  170. }
  171. /**
  172. * Indicate that the table should be dropped.
  173. *
  174. * @return \Illuminate\Support\Fluent
  175. */
  176. public function drop()
  177. {
  178. return $this->addCommand('drop');
  179. }
  180. /**
  181. * Indicate that the table should be dropped if it exists.
  182. *
  183. * @return \Illuminate\Support\Fluent
  184. */
  185. public function dropIfExists()
  186. {
  187. return $this->addCommand('dropIfExists');
  188. }
  189. /**
  190. * Indicate that the given columns should be dropped.
  191. *
  192. * @param array|mixed $columns
  193. * @return \Illuminate\Support\Fluent
  194. */
  195. public function dropColumn($columns)
  196. {
  197. $columns = is_array($columns) ? $columns : (array) func_get_args();
  198. return $this->addCommand('dropColumn', compact('columns'));
  199. }
  200. /**
  201. * Indicate that the given columns should be renamed.
  202. *
  203. * @param string $from
  204. * @param string $to
  205. * @return \Illuminate\Support\Fluent
  206. */
  207. public function renameColumn($from, $to)
  208. {
  209. return $this->addCommand('renameColumn', compact('from', 'to'));
  210. }
  211. /**
  212. * Indicate that the given primary key should be dropped.
  213. *
  214. * @param string|array $index
  215. * @return \Illuminate\Support\Fluent
  216. */
  217. public function dropPrimary($index = null)
  218. {
  219. return $this->dropIndexCommand('dropPrimary', 'primary', $index);
  220. }
  221. /**
  222. * Indicate that the given unique key should be dropped.
  223. *
  224. * @param string|array $index
  225. * @return \Illuminate\Support\Fluent
  226. */
  227. public function dropUnique($index)
  228. {
  229. return $this->dropIndexCommand('dropUnique', 'unique', $index);
  230. }
  231. /**
  232. * Indicate that the given index should be dropped.
  233. *
  234. * @param string|array $index
  235. * @return \Illuminate\Support\Fluent
  236. */
  237. public function dropIndex($index)
  238. {
  239. return $this->dropIndexCommand('dropIndex', 'index', $index);
  240. }
  241. /**
  242. * Indicate that the given foreign key should be dropped.
  243. *
  244. * @param string|array $index
  245. * @return \Illuminate\Support\Fluent
  246. */
  247. public function dropForeign($index)
  248. {
  249. return $this->dropIndexCommand('dropForeign', 'foreign', $index);
  250. }
  251. /**
  252. * Indicate that the timestamp columns should be dropped.
  253. *
  254. * @return void
  255. */
  256. public function dropTimestamps()
  257. {
  258. $this->dropColumn('created_at', 'updated_at');
  259. }
  260. /**
  261. * Indicate that the timestamp columns should be dropped.
  262. *
  263. * @return void
  264. */
  265. public function dropTimestampsTz()
  266. {
  267. $this->dropTimestamps();
  268. }
  269. /**
  270. * Indicate that the soft delete column should be dropped.
  271. *
  272. * @return void
  273. */
  274. public function dropSoftDeletes()
  275. {
  276. $this->dropColumn('deleted_at');
  277. }
  278. /**
  279. * Indicate that the remember token column should be dropped.
  280. *
  281. * @return void
  282. */
  283. public function dropRememberToken()
  284. {
  285. $this->dropColumn('remember_token');
  286. }
  287. /**
  288. * Rename the table to a given name.
  289. *
  290. * @param string $to
  291. * @return \Illuminate\Support\Fluent
  292. */
  293. public function rename($to)
  294. {
  295. return $this->addCommand('rename', compact('to'));
  296. }
  297. /**
  298. * Specify the primary key(s) for the table.
  299. *
  300. * @param string|array $columns
  301. * @param string $name
  302. * @return \Illuminate\Support\Fluent
  303. */
  304. public function primary($columns, $name = null)
  305. {
  306. return $this->indexCommand('primary', $columns, $name);
  307. }
  308. /**
  309. * Specify a unique index for the table.
  310. *
  311. * @param string|array $columns
  312. * @param string $name
  313. * @return \Illuminate\Support\Fluent
  314. */
  315. public function unique($columns, $name = null)
  316. {
  317. return $this->indexCommand('unique', $columns, $name);
  318. }
  319. /**
  320. * Specify an index for the table.
  321. *
  322. * @param string|array $columns
  323. * @param string $name
  324. * @return \Illuminate\Support\Fluent
  325. */
  326. public function index($columns, $name = null)
  327. {
  328. return $this->indexCommand('index', $columns, $name);
  329. }
  330. /**
  331. * Specify a foreign key for the table.
  332. *
  333. * @param string|array $columns
  334. * @param string $name
  335. * @return \Illuminate\Support\Fluent
  336. */
  337. public function foreign($columns, $name = null)
  338. {
  339. return $this->indexCommand('foreign', $columns, $name);
  340. }
  341. /**
  342. * Create a new auto-incrementing integer (4-byte) column on the table.
  343. *
  344. * @param string $column
  345. * @return \Illuminate\Support\Fluent
  346. */
  347. public function increments($column)
  348. {
  349. return $this->unsignedInteger($column, true);
  350. }
  351. /**
  352. * Create a new auto-incrementing small integer (2-byte) column on the table.
  353. *
  354. * @param string $column
  355. * @return \Illuminate\Support\Fluent
  356. */
  357. public function smallIncrements($column)
  358. {
  359. return $this->unsignedSmallInteger($column, true);
  360. }
  361. /**
  362. * Create a new auto-incrementing medium integer (3-byte) column on the table.
  363. *
  364. * @param string $column
  365. * @return \Illuminate\Support\Fluent
  366. */
  367. public function mediumIncrements($column)
  368. {
  369. return $this->unsignedMediumInteger($column, true);
  370. }
  371. /**
  372. * Create a new auto-incrementing big integer (8-byte) column on the table.
  373. *
  374. * @param string $column
  375. * @return \Illuminate\Support\Fluent
  376. */
  377. public function bigIncrements($column)
  378. {
  379. return $this->unsignedBigInteger($column, true);
  380. }
  381. /**
  382. * Create a new char column on the table.
  383. *
  384. * @param string $column
  385. * @param int $length
  386. * @return \Illuminate\Support\Fluent
  387. */
  388. public function char($column, $length = 255)
  389. {
  390. return $this->addColumn('char', $column, compact('length'));
  391. }
  392. /**
  393. * Create a new string column on the table.
  394. *
  395. * @param string $column
  396. * @param int $length
  397. * @return \Illuminate\Support\Fluent
  398. */
  399. public function string($column, $length = 255)
  400. {
  401. return $this->addColumn('string', $column, compact('length'));
  402. }
  403. /**
  404. * Create a new text column on the table.
  405. *
  406. * @param string $column
  407. * @return \Illuminate\Support\Fluent
  408. */
  409. public function text($column)
  410. {
  411. return $this->addColumn('text', $column);
  412. }
  413. /**
  414. * Create a new medium text column on the table.
  415. *
  416. * @param string $column
  417. * @return \Illuminate\Support\Fluent
  418. */
  419. public function mediumText($column)
  420. {
  421. return $this->addColumn('mediumText', $column);
  422. }
  423. /**
  424. * Create a new long text column on the table.
  425. *
  426. * @param string $column
  427. * @return \Illuminate\Support\Fluent
  428. */
  429. public function longText($column)
  430. {
  431. return $this->addColumn('longText', $column);
  432. }
  433. /**
  434. * Create a new integer (4-byte) column on the table.
  435. *
  436. * @param string $column
  437. * @param bool $autoIncrement
  438. * @param bool $unsigned
  439. * @return \Illuminate\Support\Fluent
  440. */
  441. public function integer($column, $autoIncrement = false, $unsigned = false)
  442. {
  443. return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned'));
  444. }
  445. /**
  446. * Create a new tiny integer (1-byte) column on the table.
  447. *
  448. * @param string $column
  449. * @param bool $autoIncrement
  450. * @param bool $unsigned
  451. * @return \Illuminate\Support\Fluent
  452. */
  453. public function tinyInteger($column, $autoIncrement = false, $unsigned = false)
  454. {
  455. return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned'));
  456. }
  457. /**
  458. * Create a new small integer (2-byte) column on the table.
  459. *
  460. * @param string $column
  461. * @param bool $autoIncrement
  462. * @param bool $unsigned
  463. * @return \Illuminate\Support\Fluent
  464. */
  465. public function smallInteger($column, $autoIncrement = false, $unsigned = false)
  466. {
  467. return $this->addColumn('smallInteger', $column, compact('autoIncrement', 'unsigned'));
  468. }
  469. /**
  470. * Create a new medium integer (3-byte) column on the table.
  471. *
  472. * @param string $column
  473. * @param bool $autoIncrement
  474. * @param bool $unsigned
  475. * @return \Illuminate\Support\Fluent
  476. */
  477. public function mediumInteger($column, $autoIncrement = false, $unsigned = false)
  478. {
  479. return $this->addColumn('mediumInteger', $column, compact('autoIncrement', 'unsigned'));
  480. }
  481. /**
  482. * Create a new big integer (8-byte) column on the table.
  483. *
  484. * @param string $column
  485. * @param bool $autoIncrement
  486. * @param bool $unsigned
  487. * @return \Illuminate\Support\Fluent
  488. */
  489. public function bigInteger($column, $autoIncrement = false, $unsigned = false)
  490. {
  491. return $this->addColumn('bigInteger', $column, compact('autoIncrement', 'unsigned'));
  492. }
  493. /**
  494. * Create a new unsigned tiny integer (1-byte) column on the table.
  495. *
  496. * @param string $column
  497. * @param bool $autoIncrement
  498. * @return \Illuminate\Support\Fluent
  499. */
  500. public function unsignedTinyInteger($column, $autoIncrement = false)
  501. {
  502. return $this->tinyInteger($column, $autoIncrement, true);
  503. }
  504. /**
  505. * Create a new unsigned small integer (2-byte) column on the table.
  506. *
  507. * @param string $column
  508. * @param bool $autoIncrement
  509. * @return \Illuminate\Support\Fluent
  510. */
  511. public function unsignedSmallInteger($column, $autoIncrement = false)
  512. {
  513. return $this->smallInteger($column, $autoIncrement, true);
  514. }
  515. /**
  516. * Create a new unsigned medium integer (3-byte) column on the table.
  517. *
  518. * @param string $column
  519. * @param bool $autoIncrement
  520. * @return \Illuminate\Support\Fluent
  521. */
  522. public function unsignedMediumInteger($column, $autoIncrement = false)
  523. {
  524. return $this->mediumInteger($column, $autoIncrement, true);
  525. }
  526. /**
  527. * Create a new unsigned integer (4-byte) column on the table.
  528. *
  529. * @param string $column
  530. * @param bool $autoIncrement
  531. * @return \Illuminate\Support\Fluent
  532. */
  533. public function unsignedInteger($column, $autoIncrement = false)
  534. {
  535. return $this->integer($column, $autoIncrement, true);
  536. }
  537. /**
  538. * Create a new unsigned big integer (8-byte) column on the table.
  539. *
  540. * @param string $column
  541. * @param bool $autoIncrement
  542. * @return \Illuminate\Support\Fluent
  543. */
  544. public function unsignedBigInteger($column, $autoIncrement = false)
  545. {
  546. return $this->bigInteger($column, $autoIncrement, true);
  547. }
  548. /**
  549. * Create a new float column on the table.
  550. *
  551. * @param string $column
  552. * @param int $total
  553. * @param int $places
  554. * @return \Illuminate\Support\Fluent
  555. */
  556. public function float($column, $total = 8, $places = 2)
  557. {
  558. return $this->addColumn('float', $column, compact('total', 'places'));
  559. }
  560. /**
  561. * Create a new double column on the table.
  562. *
  563. * @param string $column
  564. * @param int|null $total
  565. * @param int|null $places
  566. * @return \Illuminate\Support\Fluent
  567. */
  568. public function double($column, $total = null, $places = null)
  569. {
  570. return $this->addColumn('double', $column, compact('total', 'places'));
  571. }
  572. /**
  573. * Create a new decimal column on the table.
  574. *
  575. * @param string $column
  576. * @param int $total
  577. * @param int $places
  578. * @return \Illuminate\Support\Fluent
  579. */
  580. public function decimal($column, $total = 8, $places = 2)
  581. {
  582. return $this->addColumn('decimal', $column, compact('total', 'places'));
  583. }
  584. /**
  585. * Create a new boolean column on the table.
  586. *
  587. * @param string $column
  588. * @return \Illuminate\Support\Fluent
  589. */
  590. public function boolean($column)
  591. {
  592. return $this->addColumn('boolean', $column);
  593. }
  594. /**
  595. * Create a new enum column on the table.
  596. *
  597. * @param string $column
  598. * @param array $allowed
  599. * @return \Illuminate\Support\Fluent
  600. */
  601. public function enum($column, array $allowed)
  602. {
  603. return $this->addColumn('enum', $column, compact('allowed'));
  604. }
  605. /**
  606. * Create a new json column on the table.
  607. *
  608. * @param string $column
  609. * @return \Illuminate\Support\Fluent
  610. */
  611. public function json($column)
  612. {
  613. return $this->addColumn('json', $column);
  614. }
  615. /**
  616. * Create a new jsonb column on the table.
  617. *
  618. * @param string $column
  619. * @return \Illuminate\Support\Fluent
  620. */
  621. public function jsonb($column)
  622. {
  623. return $this->addColumn('jsonb', $column);
  624. }
  625. /**
  626. * Create a new date column on the table.
  627. *
  628. * @param string $column
  629. * @return \Illuminate\Support\Fluent
  630. */
  631. public function date($column)
  632. {
  633. return $this->addColumn('date', $column);
  634. }
  635. /**
  636. * Create a new date-time column on the table.
  637. *
  638. * @param string $column
  639. * @return \Illuminate\Support\Fluent
  640. */
  641. public function dateTime($column)
  642. {
  643. return $this->addColumn('dateTime', $column);
  644. }
  645. /**
  646. * Create a new date-time column (with time zone) on the table.
  647. *
  648. * @param string $column
  649. * @return \Illuminate\Support\Fluent
  650. */
  651. public function dateTimeTz($column)
  652. {
  653. return $this->addColumn('dateTimeTz', $column);
  654. }
  655. /**
  656. * Create a new time column on the table.
  657. *
  658. * @param string $column
  659. * @return \Illuminate\Support\Fluent
  660. */
  661. public function time($column)
  662. {
  663. return $this->addColumn('time', $column);
  664. }
  665. /**
  666. * Create a new time column (with time zone) on the table.
  667. *
  668. * @param string $column
  669. * @return \Illuminate\Support\Fluent
  670. */
  671. public function timeTz($column)
  672. {
  673. return $this->addColumn('timeTz', $column);
  674. }
  675. /**
  676. * Create a new timestamp column on the table.
  677. *
  678. * @param string $column
  679. * @return \Illuminate\Support\Fluent
  680. */
  681. public function timestamp($column)
  682. {
  683. return $this->addColumn('timestamp', $column);
  684. }
  685. /**
  686. * Create a new timestamp (with time zone) column on the table.
  687. *
  688. * @param string $column
  689. * @return \Illuminate\Support\Fluent
  690. */
  691. public function timestampTz($column)
  692. {
  693. return $this->addColumn('timestampTz', $column);
  694. }
  695. /**
  696. * Add nullable creation and update timestamps to the table.
  697. *
  698. * Alias for self::timestamps().
  699. *
  700. * @return void
  701. */
  702. public function nullableTimestamps()
  703. {
  704. $this->timestamps();
  705. }
  706. /**
  707. * Add nullable creation and update timestamps to the table.
  708. *
  709. * @return void
  710. */
  711. public function timestamps()
  712. {
  713. $this->timestamp('created_at')->nullable();
  714. $this->timestamp('updated_at')->nullable();
  715. }
  716. /**
  717. * Add creation and update timestampTz columns to the table.
  718. *
  719. * @return void
  720. */
  721. public function timestampsTz()
  722. {
  723. $this->timestampTz('created_at')->nullable();
  724. $this->timestampTz('updated_at')->nullable();
  725. }
  726. /**
  727. * Add a "deleted at" timestamp for the table.
  728. *
  729. * @return \Illuminate\Support\Fluent
  730. */
  731. public function softDeletes()
  732. {
  733. return $this->timestamp('deleted_at')->nullable();
  734. }
  735. /**
  736. * Create a new binary column on the table.
  737. *
  738. * @param string $column
  739. * @return \Illuminate\Support\Fluent
  740. */
  741. public function binary($column)
  742. {
  743. return $this->addColumn('binary', $column);
  744. }
  745. /**
  746. * Create a new uuid column on the table.
  747. *
  748. * @param string $column
  749. * @return \Illuminate\Support\Fluent
  750. */
  751. public function uuid($column)
  752. {
  753. return $this->addColumn('uuid', $column);
  754. }
  755. /**
  756. * Create a new IP address column on the table.
  757. *
  758. * @param string $column
  759. * @return \Illuminate\Support\Fluent
  760. */
  761. public function ipAddress($column)
  762. {
  763. return $this->addColumn('ipAddress', $column);
  764. }
  765. /**
  766. * Create a new MAC address column on the table.
  767. *
  768. * @param string $column
  769. * @return \Illuminate\Support\Fluent
  770. */
  771. public function macAddress($column)
  772. {
  773. return $this->addColumn('macAddress', $column);
  774. }
  775. /**
  776. * Add the proper columns for a polymorphic table.
  777. *
  778. * @param string $name
  779. * @param string|null $indexName
  780. * @return void
  781. */
  782. public function morphs($name, $indexName = null)
  783. {
  784. $this->unsignedInteger("{$name}_id");
  785. $this->string("{$name}_type");
  786. $this->index(["{$name}_id", "{$name}_type"], $indexName);
  787. }
  788. /**
  789. * Adds the `remember_token` column to the table.
  790. *
  791. * @return \Illuminate\Support\Fluent
  792. */
  793. public function rememberToken()
  794. {
  795. return $this->string('remember_token', 100)->nullable();
  796. }
  797. /**
  798. * Create a new drop index command on the blueprint.
  799. *
  800. * @param string $command
  801. * @param string $type
  802. * @param string|array $index
  803. * @return \Illuminate\Support\Fluent
  804. */
  805. protected function dropIndexCommand($command, $type, $index)
  806. {
  807. $columns = [];
  808. // If the given "index" is actually an array of columns, the developer means
  809. // to drop an index merely by specifying the columns involved without the
  810. // conventional name, so we will build the index name from the columns.
  811. if (is_array($index)) {
  812. $columns = $index;
  813. $index = $this->createIndexName($type, $columns);
  814. }
  815. return $this->indexCommand($command, $columns, $index);
  816. }
  817. /**
  818. * Add a new index command to the blueprint.
  819. *
  820. * @param string $type
  821. * @param string|array $columns
  822. * @param string $index
  823. * @return \Illuminate\Support\Fluent
  824. */
  825. protected function indexCommand($type, $columns, $index)
  826. {
  827. $columns = (array) $columns;
  828. // If no name was specified for this index, we will create one using a basic
  829. // convention of the table name, followed by the columns, followed by an
  830. // index type, such as primary or index, which makes the index unique.
  831. if (is_null($index)) {
  832. $index = $this->createIndexName($type, $columns);
  833. }
  834. return $this->addCommand($type, compact('index', 'columns'));
  835. }
  836. /**
  837. * Create a default index name for the table.
  838. *
  839. * @param string $type
  840. * @param array $columns
  841. * @return string
  842. */
  843. protected function createIndexName($type, array $columns)
  844. {
  845. $index = strtolower($this->table.'_'.implode('_', $columns).'_'.$type);
  846. return str_replace(['-', '.'], '_', $index);
  847. }
  848. /**
  849. * Add a new column to the blueprint.
  850. *
  851. * @param string $type
  852. * @param string $name
  853. * @param array $parameters
  854. * @return \Illuminate\Support\Fluent
  855. */
  856. public function addColumn($type, $name, array $parameters = [])
  857. {
  858. $attributes = array_merge(compact('type', 'name'), $parameters);
  859. $this->columns[] = $column = new Fluent($attributes);
  860. return $column;
  861. }
  862. /**
  863. * Remove a column from the schema blueprint.
  864. *
  865. * @param string $name
  866. * @return $this
  867. */
  868. public function removeColumn($name)
  869. {
  870. $this->columns = array_values(array_filter($this->columns, function ($c) use ($name) {
  871. return $c['attributes']['name'] != $name;
  872. }));
  873. return $this;
  874. }
  875. /**
  876. * Add a new command to the blueprint.
  877. *
  878. * @param string $name
  879. * @param array $parameters
  880. * @return \Illuminate\Support\Fluent
  881. */
  882. protected function addCommand($name, array $parameters = [])
  883. {
  884. $this->commands[] = $command = $this->createCommand($name, $parameters);
  885. return $command;
  886. }
  887. /**
  888. * Create a new Fluent command.
  889. *
  890. * @param string $name
  891. * @param array $parameters
  892. * @return \Illuminate\Support\Fluent
  893. */
  894. protected function createCommand($name, array $parameters = [])
  895. {
  896. return new Fluent(array_merge(compact('name'), $parameters));
  897. }
  898. /**
  899. * Get the table the blueprint describes.
  900. *
  901. * @return string
  902. */
  903. public function getTable()
  904. {
  905. return $this->table;
  906. }
  907. /**
  908. * Get the columns on the blueprint.
  909. *
  910. * @return array
  911. */
  912. public function getColumns()
  913. {
  914. return $this->columns;
  915. }
  916. /**
  917. * Get the commands on the blueprint.
  918. *
  919. * @return array
  920. */
  921. public function getCommands()
  922. {
  923. return $this->commands;
  924. }
  925. /**
  926. * Get the columns on the blueprint that should be added.
  927. *
  928. * @return array
  929. */
  930. public function getAddedColumns()
  931. {
  932. return array_filter($this->columns, function ($column) {
  933. return ! $column->change;
  934. });
  935. }
  936. /**
  937. * Get the columns on the blueprint that should be changed.
  938. *
  939. * @return array
  940. */
  941. public function getChangedColumns()
  942. {
  943. return array_filter($this->columns, function ($column) {
  944. return (bool) $column->change;
  945. });
  946. }
  947. }