Blueprint.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. <?php
  2. namespace Illuminate\Database\Schema;
  3. use Closure;
  4. use Illuminate\Database\Connection;
  5. use Illuminate\Database\Eloquent\Concerns\HasUlids;
  6. use Illuminate\Database\Query\Expression;
  7. use Illuminate\Database\Schema\Grammars\Grammar;
  8. use Illuminate\Database\Schema\Grammars\MySqlGrammar;
  9. use Illuminate\Support\Fluent;
  10. use Illuminate\Support\Traits\Macroable;
  11. class Blueprint
  12. {
  13. use Macroable;
  14. /**
  15. * The table the blueprint describes.
  16. *
  17. * @var string
  18. */
  19. protected $table;
  20. /**
  21. * The prefix of the table.
  22. *
  23. * @var string
  24. */
  25. protected $prefix;
  26. /**
  27. * The columns that should be added to the table.
  28. *
  29. * @var \Illuminate\Database\Schema\ColumnDefinition[]
  30. */
  31. protected $columns = [];
  32. /**
  33. * The commands that should be run for the table.
  34. *
  35. * @var \Illuminate\Support\Fluent[]
  36. */
  37. protected $commands = [];
  38. /**
  39. * The storage engine that should be used for the table.
  40. *
  41. * @var string
  42. */
  43. public $engine;
  44. /**
  45. * The default character set that should be used for the table.
  46. *
  47. * @var string
  48. */
  49. public $charset;
  50. /**
  51. * The collation that should be used for the table.
  52. *
  53. * @var string
  54. */
  55. public $collation;
  56. /**
  57. * Whether to make the table temporary.
  58. *
  59. * @var bool
  60. */
  61. public $temporary = false;
  62. /**
  63. * The column to add new columns after.
  64. *
  65. * @var string
  66. */
  67. public $after;
  68. /**
  69. * Create a new schema blueprint.
  70. *
  71. * @param string $table
  72. * @param \Closure|null $callback
  73. * @param string $prefix
  74. * @return void
  75. */
  76. public function __construct($table, ?Closure $callback = null, $prefix = '')
  77. {
  78. $this->table = $table;
  79. $this->prefix = $prefix;
  80. if (! is_null($callback)) {
  81. $callback($this);
  82. }
  83. }
  84. /**
  85. * Execute the blueprint against the database.
  86. *
  87. * @param \Illuminate\Database\Connection $connection
  88. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  89. * @return void
  90. */
  91. public function build(Connection $connection, Grammar $grammar)
  92. {
  93. foreach ($this->toSql($connection, $grammar) as $statement) {
  94. $connection->statement($statement);
  95. }
  96. }
  97. /**
  98. * Get the raw SQL statements for the blueprint.
  99. *
  100. * @param \Illuminate\Database\Connection $connection
  101. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  102. * @return array
  103. */
  104. public function toSql(Connection $connection, Grammar $grammar)
  105. {
  106. $this->addImpliedCommands($connection, $grammar);
  107. $statements = [];
  108. // Each type of command has a corresponding compiler function on the schema
  109. // grammar which is used to build the necessary SQL statements to build
  110. // the blueprint element, so we'll just call that compilers function.
  111. $this->ensureCommandsAreValid($connection);
  112. foreach ($this->commands as $command) {
  113. if ($command->shouldBeSkipped) {
  114. continue;
  115. }
  116. $method = 'compile'.ucfirst($command->name);
  117. if (method_exists($grammar, $method) || $grammar::hasMacro($method)) {
  118. if (! is_null($sql = $grammar->$method($this, $command, $connection))) {
  119. $statements = array_merge($statements, (array) $sql);
  120. }
  121. }
  122. }
  123. return $statements;
  124. }
  125. /**
  126. * Ensure the commands on the blueprint are valid for the connection type.
  127. *
  128. * @param \Illuminate\Database\Connection $connection
  129. * @return void
  130. *
  131. * @throws \BadMethodCallException
  132. */
  133. protected function ensureCommandsAreValid(Connection $connection)
  134. {
  135. //
  136. }
  137. /**
  138. * Get all of the commands matching the given names.
  139. *
  140. * @param array $names
  141. * @return \Illuminate\Support\Collection
  142. */
  143. protected function commandsNamed(array $names)
  144. {
  145. return collect($this->commands)->filter(function ($command) use ($names) {
  146. return in_array($command->name, $names);
  147. });
  148. }
  149. /**
  150. * Add the commands that are implied by the blueprint's state.
  151. *
  152. * @param \Illuminate\Database\Connection $connection
  153. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  154. * @return void
  155. */
  156. protected function addImpliedCommands(Connection $connection, Grammar $grammar)
  157. {
  158. if (count($this->getAddedColumns()) > 0 && ! $this->creating()) {
  159. array_unshift($this->commands, $this->createCommand('add'));
  160. }
  161. if (count($this->getChangedColumns()) > 0 && ! $this->creating()) {
  162. array_unshift($this->commands, $this->createCommand('change'));
  163. }
  164. $this->addFluentIndexes($connection, $grammar);
  165. $this->addFluentCommands($connection, $grammar);
  166. }
  167. /**
  168. * Add the index commands fluently specified on columns.
  169. *
  170. * @param \Illuminate\Database\Connection $connection
  171. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  172. * @return void
  173. */
  174. protected function addFluentIndexes(Connection $connection, Grammar $grammar)
  175. {
  176. foreach ($this->columns as $column) {
  177. foreach (['primary', 'unique', 'index', 'fulltext', 'fullText', 'spatialIndex'] as $index) {
  178. // If the column is supposed to be changed to an auto increment column and
  179. // the specified index is primary, there is no need to add a command on
  180. // MySQL, as it will be handled during the column definition instead.
  181. if ($index === 'primary' && $column->autoIncrement && $column->change && $grammar instanceof MySqlGrammar) {
  182. continue 2;
  183. }
  184. // If the index has been specified on the given column, but is simply equal
  185. // to "true" (boolean), no name has been specified for this index so the
  186. // index method can be called without a name and it will generate one.
  187. if ($column->{$index} === true) {
  188. $this->{$index}($column->name);
  189. $column->{$index} = null;
  190. continue 2;
  191. }
  192. // If the index has been specified on the given column, but it equals false
  193. // and the column is supposed to be changed, we will call the drop index
  194. // method with an array of column to drop it by its conventional name.
  195. elseif ($column->{$index} === false && $column->change) {
  196. $this->{'drop'.ucfirst($index)}([$column->name]);
  197. $column->{$index} = null;
  198. continue 2;
  199. }
  200. // If the index has been specified on the given column, and it has a string
  201. // value, we'll go ahead and call the index method and pass the name for
  202. // the index since the developer specified the explicit name for this.
  203. elseif (isset($column->{$index})) {
  204. $this->{$index}($column->name, $column->{$index});
  205. $column->{$index} = null;
  206. continue 2;
  207. }
  208. }
  209. }
  210. }
  211. /**
  212. * Add the fluent commands specified on any columns.
  213. *
  214. * @param \Illuminate\Database\Connection $connection
  215. * @param \Illuminate\Database\Schema\Grammars\Grammar $grammar
  216. * @return void
  217. */
  218. public function addFluentCommands(Connection $connection, Grammar $grammar)
  219. {
  220. foreach ($this->columns as $column) {
  221. foreach ($grammar->getFluentCommands() as $commandName) {
  222. $this->addCommand($commandName, compact('column'));
  223. }
  224. }
  225. }
  226. /**
  227. * Determine if the blueprint has a create command.
  228. *
  229. * @return bool
  230. */
  231. public function creating()
  232. {
  233. return collect($this->commands)->contains(function ($command) {
  234. return $command->name === 'create';
  235. });
  236. }
  237. /**
  238. * Indicate that the table needs to be created.
  239. *
  240. * @return \Illuminate\Support\Fluent
  241. */
  242. public function create()
  243. {
  244. return $this->addCommand('create');
  245. }
  246. /**
  247. * Specify the storage engine that should be used for the table.
  248. *
  249. * @param string $engine
  250. * @return void
  251. */
  252. public function engine($engine)
  253. {
  254. $this->engine = $engine;
  255. }
  256. /**
  257. * Specify that the InnoDB storage engine should be used for the table (MySQL only).
  258. *
  259. * @return void
  260. */
  261. public function innoDb()
  262. {
  263. $this->engine('InnoDB');
  264. }
  265. /**
  266. * Specify the character set that should be used for the table.
  267. *
  268. * @param string $charset
  269. * @return void
  270. */
  271. public function charset($charset)
  272. {
  273. $this->charset = $charset;
  274. }
  275. /**
  276. * Specify the collation that should be used for the table.
  277. *
  278. * @param string $collation
  279. * @return void
  280. */
  281. public function collation($collation)
  282. {
  283. $this->collation = $collation;
  284. }
  285. /**
  286. * Indicate that the table needs to be temporary.
  287. *
  288. * @return void
  289. */
  290. public function temporary()
  291. {
  292. $this->temporary = true;
  293. }
  294. /**
  295. * Indicate that the table should be dropped.
  296. *
  297. * @return \Illuminate\Support\Fluent
  298. */
  299. public function drop()
  300. {
  301. return $this->addCommand('drop');
  302. }
  303. /**
  304. * Indicate that the table should be dropped if it exists.
  305. *
  306. * @return \Illuminate\Support\Fluent
  307. */
  308. public function dropIfExists()
  309. {
  310. return $this->addCommand('dropIfExists');
  311. }
  312. /**
  313. * Indicate that the given columns should be dropped.
  314. *
  315. * @param array|mixed $columns
  316. * @return \Illuminate\Support\Fluent
  317. */
  318. public function dropColumn($columns)
  319. {
  320. $columns = is_array($columns) ? $columns : func_get_args();
  321. return $this->addCommand('dropColumn', compact('columns'));
  322. }
  323. /**
  324. * Indicate that the given columns should be renamed.
  325. *
  326. * @param string $from
  327. * @param string $to
  328. * @return \Illuminate\Support\Fluent
  329. */
  330. public function renameColumn($from, $to)
  331. {
  332. return $this->addCommand('renameColumn', compact('from', 'to'));
  333. }
  334. /**
  335. * Indicate that the given primary key should be dropped.
  336. *
  337. * @param string|array|null $index
  338. * @return \Illuminate\Support\Fluent
  339. */
  340. public function dropPrimary($index = null)
  341. {
  342. return $this->dropIndexCommand('dropPrimary', 'primary', $index);
  343. }
  344. /**
  345. * Indicate that the given unique key should be dropped.
  346. *
  347. * @param string|array $index
  348. * @return \Illuminate\Support\Fluent
  349. */
  350. public function dropUnique($index)
  351. {
  352. return $this->dropIndexCommand('dropUnique', 'unique', $index);
  353. }
  354. /**
  355. * Indicate that the given index should be dropped.
  356. *
  357. * @param string|array $index
  358. * @return \Illuminate\Support\Fluent
  359. */
  360. public function dropIndex($index)
  361. {
  362. return $this->dropIndexCommand('dropIndex', 'index', $index);
  363. }
  364. /**
  365. * Indicate that the given fulltext index should be dropped.
  366. *
  367. * @param string|array $index
  368. * @return \Illuminate\Support\Fluent
  369. */
  370. public function dropFullText($index)
  371. {
  372. return $this->dropIndexCommand('dropFullText', 'fulltext', $index);
  373. }
  374. /**
  375. * Indicate that the given spatial index should be dropped.
  376. *
  377. * @param string|array $index
  378. * @return \Illuminate\Support\Fluent
  379. */
  380. public function dropSpatialIndex($index)
  381. {
  382. return $this->dropIndexCommand('dropSpatialIndex', 'spatialIndex', $index);
  383. }
  384. /**
  385. * Indicate that the given foreign key should be dropped.
  386. *
  387. * @param string|array $index
  388. * @return \Illuminate\Support\Fluent
  389. */
  390. public function dropForeign($index)
  391. {
  392. return $this->dropIndexCommand('dropForeign', 'foreign', $index);
  393. }
  394. /**
  395. * Indicate that the given column and foreign key should be dropped.
  396. *
  397. * @param string $column
  398. * @return \Illuminate\Support\Fluent
  399. */
  400. public function dropConstrainedForeignId($column)
  401. {
  402. $this->dropForeign([$column]);
  403. return $this->dropColumn($column);
  404. }
  405. /**
  406. * Indicate that the given foreign key should be dropped.
  407. *
  408. * @param \Illuminate\Database\Eloquent\Model|string $model
  409. * @param string|null $column
  410. * @return \Illuminate\Support\Fluent
  411. */
  412. public function dropForeignIdFor($model, $column = null)
  413. {
  414. if (is_string($model)) {
  415. $model = new $model;
  416. }
  417. return $this->dropForeign([$column ?: $model->getForeignKey()]);
  418. }
  419. /**
  420. * Indicate that the given foreign key should be dropped.
  421. *
  422. * @param \Illuminate\Database\Eloquent\Model|string $model
  423. * @param string|null $column
  424. * @return \Illuminate\Support\Fluent
  425. */
  426. public function dropConstrainedForeignIdFor($model, $column = null)
  427. {
  428. if (is_string($model)) {
  429. $model = new $model;
  430. }
  431. return $this->dropConstrainedForeignId($column ?: $model->getForeignKey());
  432. }
  433. /**
  434. * Indicate that the given indexes should be renamed.
  435. *
  436. * @param string $from
  437. * @param string $to
  438. * @return \Illuminate\Support\Fluent
  439. */
  440. public function renameIndex($from, $to)
  441. {
  442. return $this->addCommand('renameIndex', compact('from', 'to'));
  443. }
  444. /**
  445. * Indicate that the timestamp columns should be dropped.
  446. *
  447. * @return void
  448. */
  449. public function dropTimestamps()
  450. {
  451. $this->dropColumn('created_at', 'updated_at');
  452. }
  453. /**
  454. * Indicate that the timestamp columns should be dropped.
  455. *
  456. * @return void
  457. */
  458. public function dropTimestampsTz()
  459. {
  460. $this->dropTimestamps();
  461. }
  462. /**
  463. * Indicate that the soft delete column should be dropped.
  464. *
  465. * @param string $column
  466. * @return void
  467. */
  468. public function dropSoftDeletes($column = 'deleted_at')
  469. {
  470. $this->dropColumn($column);
  471. }
  472. /**
  473. * Indicate that the soft delete column should be dropped.
  474. *
  475. * @param string $column
  476. * @return void
  477. */
  478. public function dropSoftDeletesTz($column = 'deleted_at')
  479. {
  480. $this->dropSoftDeletes($column);
  481. }
  482. /**
  483. * Indicate that the remember token column should be dropped.
  484. *
  485. * @return void
  486. */
  487. public function dropRememberToken()
  488. {
  489. $this->dropColumn('remember_token');
  490. }
  491. /**
  492. * Indicate that the polymorphic columns should be dropped.
  493. *
  494. * @param string $name
  495. * @param string|null $indexName
  496. * @return void
  497. */
  498. public function dropMorphs($name, $indexName = null)
  499. {
  500. $this->dropIndex($indexName ?: $this->createIndexName('index', ["{$name}_type", "{$name}_id"]));
  501. $this->dropColumn("{$name}_type", "{$name}_id");
  502. }
  503. /**
  504. * Rename the table to a given name.
  505. *
  506. * @param string $to
  507. * @return \Illuminate\Support\Fluent
  508. */
  509. public function rename($to)
  510. {
  511. return $this->addCommand('rename', compact('to'));
  512. }
  513. /**
  514. * Specify the primary key(s) for the table.
  515. *
  516. * @param string|array $columns
  517. * @param string|null $name
  518. * @param string|null $algorithm
  519. * @return \Illuminate\Database\Schema\IndexDefinition
  520. */
  521. public function primary($columns, $name = null, $algorithm = null)
  522. {
  523. return $this->indexCommand('primary', $columns, $name, $algorithm);
  524. }
  525. /**
  526. * Specify a unique index for the table.
  527. *
  528. * @param string|array $columns
  529. * @param string|null $name
  530. * @param string|null $algorithm
  531. * @return \Illuminate\Database\Schema\IndexDefinition
  532. */
  533. public function unique($columns, $name = null, $algorithm = null)
  534. {
  535. return $this->indexCommand('unique', $columns, $name, $algorithm);
  536. }
  537. /**
  538. * Specify an index for the table.
  539. *
  540. * @param string|array $columns
  541. * @param string|null $name
  542. * @param string|null $algorithm
  543. * @return \Illuminate\Database\Schema\IndexDefinition
  544. */
  545. public function index($columns, $name = null, $algorithm = null)
  546. {
  547. return $this->indexCommand('index', $columns, $name, $algorithm);
  548. }
  549. /**
  550. * Specify an fulltext for the table.
  551. *
  552. * @param string|array $columns
  553. * @param string|null $name
  554. * @param string|null $algorithm
  555. * @return \Illuminate\Database\Schema\IndexDefinition
  556. */
  557. public function fullText($columns, $name = null, $algorithm = null)
  558. {
  559. return $this->indexCommand('fulltext', $columns, $name, $algorithm);
  560. }
  561. /**
  562. * Specify a spatial index for the table.
  563. *
  564. * @param string|array $columns
  565. * @param string|null $name
  566. * @return \Illuminate\Database\Schema\IndexDefinition
  567. */
  568. public function spatialIndex($columns, $name = null)
  569. {
  570. return $this->indexCommand('spatialIndex', $columns, $name);
  571. }
  572. /**
  573. * Specify a raw index for the table.
  574. *
  575. * @param string $expression
  576. * @param string $name
  577. * @return \Illuminate\Database\Schema\IndexDefinition
  578. */
  579. public function rawIndex($expression, $name)
  580. {
  581. return $this->index([new Expression($expression)], $name);
  582. }
  583. /**
  584. * Specify a foreign key for the table.
  585. *
  586. * @param string|array $columns
  587. * @param string|null $name
  588. * @return \Illuminate\Database\Schema\ForeignKeyDefinition
  589. */
  590. public function foreign($columns, $name = null)
  591. {
  592. $command = new ForeignKeyDefinition(
  593. $this->indexCommand('foreign', $columns, $name)->getAttributes()
  594. );
  595. $this->commands[count($this->commands) - 1] = $command;
  596. return $command;
  597. }
  598. /**
  599. * Create a new auto-incrementing big integer (8-byte) column on the table.
  600. *
  601. * @param string $column
  602. * @return \Illuminate\Database\Schema\ColumnDefinition
  603. */
  604. public function id($column = 'id')
  605. {
  606. return $this->bigIncrements($column);
  607. }
  608. /**
  609. * Create a new auto-incrementing integer (4-byte) column on the table.
  610. *
  611. * @param string $column
  612. * @return \Illuminate\Database\Schema\ColumnDefinition
  613. */
  614. public function increments($column)
  615. {
  616. return $this->unsignedInteger($column, true);
  617. }
  618. /**
  619. * Create a new auto-incrementing integer (4-byte) column on the table.
  620. *
  621. * @param string $column
  622. * @return \Illuminate\Database\Schema\ColumnDefinition
  623. */
  624. public function integerIncrements($column)
  625. {
  626. return $this->unsignedInteger($column, true);
  627. }
  628. /**
  629. * Create a new auto-incrementing tiny integer (1-byte) column on the table.
  630. *
  631. * @param string $column
  632. * @return \Illuminate\Database\Schema\ColumnDefinition
  633. */
  634. public function tinyIncrements($column)
  635. {
  636. return $this->unsignedTinyInteger($column, true);
  637. }
  638. /**
  639. * Create a new auto-incrementing small integer (2-byte) column on the table.
  640. *
  641. * @param string $column
  642. * @return \Illuminate\Database\Schema\ColumnDefinition
  643. */
  644. public function smallIncrements($column)
  645. {
  646. return $this->unsignedSmallInteger($column, true);
  647. }
  648. /**
  649. * Create a new auto-incrementing medium integer (3-byte) column on the table.
  650. *
  651. * @param string $column
  652. * @return \Illuminate\Database\Schema\ColumnDefinition
  653. */
  654. public function mediumIncrements($column)
  655. {
  656. return $this->unsignedMediumInteger($column, true);
  657. }
  658. /**
  659. * Create a new auto-incrementing big integer (8-byte) column on the table.
  660. *
  661. * @param string $column
  662. * @return \Illuminate\Database\Schema\ColumnDefinition
  663. */
  664. public function bigIncrements($column)
  665. {
  666. return $this->unsignedBigInteger($column, true);
  667. }
  668. /**
  669. * Create a new char column on the table.
  670. *
  671. * @param string $column
  672. * @param int|null $length
  673. * @return \Illuminate\Database\Schema\ColumnDefinition
  674. */
  675. public function char($column, $length = null)
  676. {
  677. $length = ! is_null($length) ? $length : Builder::$defaultStringLength;
  678. return $this->addColumn('char', $column, compact('length'));
  679. }
  680. /**
  681. * Create a new string column on the table.
  682. *
  683. * @param string $column
  684. * @param int|null $length
  685. * @return \Illuminate\Database\Schema\ColumnDefinition
  686. */
  687. public function string($column, $length = null)
  688. {
  689. $length = $length ?: Builder::$defaultStringLength;
  690. return $this->addColumn('string', $column, compact('length'));
  691. }
  692. /**
  693. * Create a new tiny text column on the table.
  694. *
  695. * @param string $column
  696. * @return \Illuminate\Database\Schema\ColumnDefinition
  697. */
  698. public function tinyText($column)
  699. {
  700. return $this->addColumn('tinyText', $column);
  701. }
  702. /**
  703. * Create a new text column on the table.
  704. *
  705. * @param string $column
  706. * @return \Illuminate\Database\Schema\ColumnDefinition
  707. */
  708. public function text($column)
  709. {
  710. return $this->addColumn('text', $column);
  711. }
  712. /**
  713. * Create a new medium text column on the table.
  714. *
  715. * @param string $column
  716. * @return \Illuminate\Database\Schema\ColumnDefinition
  717. */
  718. public function mediumText($column)
  719. {
  720. return $this->addColumn('mediumText', $column);
  721. }
  722. /**
  723. * Create a new long text column on the table.
  724. *
  725. * @param string $column
  726. * @return \Illuminate\Database\Schema\ColumnDefinition
  727. */
  728. public function longText($column)
  729. {
  730. return $this->addColumn('longText', $column);
  731. }
  732. /**
  733. * Create a new integer (4-byte) column on the table.
  734. *
  735. * @param string $column
  736. * @param bool $autoIncrement
  737. * @param bool $unsigned
  738. * @return \Illuminate\Database\Schema\ColumnDefinition
  739. */
  740. public function integer($column, $autoIncrement = false, $unsigned = false)
  741. {
  742. return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned'));
  743. }
  744. /**
  745. * Create a new tiny integer (1-byte) column on the table.
  746. *
  747. * @param string $column
  748. * @param bool $autoIncrement
  749. * @param bool $unsigned
  750. * @return \Illuminate\Database\Schema\ColumnDefinition
  751. */
  752. public function tinyInteger($column, $autoIncrement = false, $unsigned = false)
  753. {
  754. return $this->addColumn('tinyInteger', $column, compact('autoIncrement', 'unsigned'));
  755. }
  756. /**
  757. * Create a new small integer (2-byte) column on the table.
  758. *
  759. * @param string $column
  760. * @param bool $autoIncrement
  761. * @param bool $unsigned
  762. * @return \Illuminate\Database\Schema\ColumnDefinition
  763. */
  764. public function smallInteger($column, $autoIncrement = false, $unsigned = false)
  765. {
  766. return $this->addColumn('smallInteger', $column, compact('autoIncrement', 'unsigned'));
  767. }
  768. /**
  769. * Create a new medium integer (3-byte) column on the table.
  770. *
  771. * @param string $column
  772. * @param bool $autoIncrement
  773. * @param bool $unsigned
  774. * @return \Illuminate\Database\Schema\ColumnDefinition
  775. */
  776. public function mediumInteger($column, $autoIncrement = false, $unsigned = false)
  777. {
  778. return $this->addColumn('mediumInteger', $column, compact('autoIncrement', 'unsigned'));
  779. }
  780. /**
  781. * Create a new big integer (8-byte) column on the table.
  782. *
  783. * @param string $column
  784. * @param bool $autoIncrement
  785. * @param bool $unsigned
  786. * @return \Illuminate\Database\Schema\ColumnDefinition
  787. */
  788. public function bigInteger($column, $autoIncrement = false, $unsigned = false)
  789. {
  790. return $this->addColumn('bigInteger', $column, compact('autoIncrement', 'unsigned'));
  791. }
  792. /**
  793. * Create a new unsigned integer (4-byte) column on the table.
  794. *
  795. * @param string $column
  796. * @param bool $autoIncrement
  797. * @return \Illuminate\Database\Schema\ColumnDefinition
  798. */
  799. public function unsignedInteger($column, $autoIncrement = false)
  800. {
  801. return $this->integer($column, $autoIncrement, true);
  802. }
  803. /**
  804. * Create a new unsigned tiny integer (1-byte) column on the table.
  805. *
  806. * @param string $column
  807. * @param bool $autoIncrement
  808. * @return \Illuminate\Database\Schema\ColumnDefinition
  809. */
  810. public function unsignedTinyInteger($column, $autoIncrement = false)
  811. {
  812. return $this->tinyInteger($column, $autoIncrement, true);
  813. }
  814. /**
  815. * Create a new unsigned small integer (2-byte) column on the table.
  816. *
  817. * @param string $column
  818. * @param bool $autoIncrement
  819. * @return \Illuminate\Database\Schema\ColumnDefinition
  820. */
  821. public function unsignedSmallInteger($column, $autoIncrement = false)
  822. {
  823. return $this->smallInteger($column, $autoIncrement, true);
  824. }
  825. /**
  826. * Create a new unsigned medium integer (3-byte) column on the table.
  827. *
  828. * @param string $column
  829. * @param bool $autoIncrement
  830. * @return \Illuminate\Database\Schema\ColumnDefinition
  831. */
  832. public function unsignedMediumInteger($column, $autoIncrement = false)
  833. {
  834. return $this->mediumInteger($column, $autoIncrement, true);
  835. }
  836. /**
  837. * Create a new unsigned big integer (8-byte) column on the table.
  838. *
  839. * @param string $column
  840. * @param bool $autoIncrement
  841. * @return \Illuminate\Database\Schema\ColumnDefinition
  842. */
  843. public function unsignedBigInteger($column, $autoIncrement = false)
  844. {
  845. return $this->bigInteger($column, $autoIncrement, true);
  846. }
  847. /**
  848. * Create a new unsigned big integer (8-byte) column on the table.
  849. *
  850. * @param string $column
  851. * @return \Illuminate\Database\Schema\ForeignIdColumnDefinition
  852. */
  853. public function foreignId($column)
  854. {
  855. return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [
  856. 'type' => 'bigInteger',
  857. 'name' => $column,
  858. 'autoIncrement' => false,
  859. 'unsigned' => true,
  860. ]));
  861. }
  862. /**
  863. * Create a foreign ID column for the given model.
  864. *
  865. * @param \Illuminate\Database\Eloquent\Model|string $model
  866. * @param string|null $column
  867. * @return \Illuminate\Database\Schema\ForeignIdColumnDefinition
  868. */
  869. public function foreignIdFor($model, $column = null)
  870. {
  871. if (is_string($model)) {
  872. $model = new $model;
  873. }
  874. $column = $column ?: $model->getForeignKey();
  875. if ($model->getKeyType() === 'int' && $model->getIncrementing()) {
  876. return $this->foreignId($column);
  877. }
  878. $modelTraits = class_uses_recursive($model);
  879. if (in_array(HasUlids::class, $modelTraits, true)) {
  880. return $this->foreignUlid($column);
  881. }
  882. return $this->foreignUuid($column);
  883. }
  884. /**
  885. * Create a new float column on the table.
  886. *
  887. * @param string $column
  888. * @param int $precision
  889. * @return \Illuminate\Database\Schema\ColumnDefinition
  890. */
  891. public function float($column, $precision = 53)
  892. {
  893. return $this->addColumn('float', $column, compact('precision'));
  894. }
  895. /**
  896. * Create a new double column on the table.
  897. *
  898. * @param string $column
  899. * @return \Illuminate\Database\Schema\ColumnDefinition
  900. */
  901. public function double($column)
  902. {
  903. return $this->addColumn('double', $column);
  904. }
  905. /**
  906. * Create a new decimal column on the table.
  907. *
  908. * @param string $column
  909. * @param int $total
  910. * @param int $places
  911. * @return \Illuminate\Database\Schema\ColumnDefinition
  912. */
  913. public function decimal($column, $total = 8, $places = 2)
  914. {
  915. return $this->addColumn('decimal', $column, compact('total', 'places'));
  916. }
  917. /**
  918. * Create a new boolean column on the table.
  919. *
  920. * @param string $column
  921. * @return \Illuminate\Database\Schema\ColumnDefinition
  922. */
  923. public function boolean($column)
  924. {
  925. return $this->addColumn('boolean', $column);
  926. }
  927. /**
  928. * Create a new enum column on the table.
  929. *
  930. * @param string $column
  931. * @param array $allowed
  932. * @return \Illuminate\Database\Schema\ColumnDefinition
  933. */
  934. public function enum($column, array $allowed)
  935. {
  936. return $this->addColumn('enum', $column, compact('allowed'));
  937. }
  938. /**
  939. * Create a new set column on the table.
  940. *
  941. * @param string $column
  942. * @param array $allowed
  943. * @return \Illuminate\Database\Schema\ColumnDefinition
  944. */
  945. public function set($column, array $allowed)
  946. {
  947. return $this->addColumn('set', $column, compact('allowed'));
  948. }
  949. /**
  950. * Create a new json column on the table.
  951. *
  952. * @param string $column
  953. * @return \Illuminate\Database\Schema\ColumnDefinition
  954. */
  955. public function json($column)
  956. {
  957. return $this->addColumn('json', $column);
  958. }
  959. /**
  960. * Create a new jsonb column on the table.
  961. *
  962. * @param string $column
  963. * @return \Illuminate\Database\Schema\ColumnDefinition
  964. */
  965. public function jsonb($column)
  966. {
  967. return $this->addColumn('jsonb', $column);
  968. }
  969. /**
  970. * Create a new date column on the table.
  971. *
  972. * @param string $column
  973. * @return \Illuminate\Database\Schema\ColumnDefinition
  974. */
  975. public function date($column)
  976. {
  977. return $this->addColumn('date', $column);
  978. }
  979. /**
  980. * Create a new date-time column on the table.
  981. *
  982. * @param string $column
  983. * @param int|null $precision
  984. * @return \Illuminate\Database\Schema\ColumnDefinition
  985. */
  986. public function dateTime($column, $precision = 0)
  987. {
  988. return $this->addColumn('dateTime', $column, compact('precision'));
  989. }
  990. /**
  991. * Create a new date-time column (with time zone) on the table.
  992. *
  993. * @param string $column
  994. * @param int|null $precision
  995. * @return \Illuminate\Database\Schema\ColumnDefinition
  996. */
  997. public function dateTimeTz($column, $precision = 0)
  998. {
  999. return $this->addColumn('dateTimeTz', $column, compact('precision'));
  1000. }
  1001. /**
  1002. * Create a new time column on the table.
  1003. *
  1004. * @param string $column
  1005. * @param int|null $precision
  1006. * @return \Illuminate\Database\Schema\ColumnDefinition
  1007. */
  1008. public function time($column, $precision = 0)
  1009. {
  1010. return $this->addColumn('time', $column, compact('precision'));
  1011. }
  1012. /**
  1013. * Create a new time column (with time zone) on the table.
  1014. *
  1015. * @param string $column
  1016. * @param int|null $precision
  1017. * @return \Illuminate\Database\Schema\ColumnDefinition
  1018. */
  1019. public function timeTz($column, $precision = 0)
  1020. {
  1021. return $this->addColumn('timeTz', $column, compact('precision'));
  1022. }
  1023. /**
  1024. * Create a new timestamp column on the table.
  1025. *
  1026. * @param string $column
  1027. * @param int|null $precision
  1028. * @return \Illuminate\Database\Schema\ColumnDefinition
  1029. */
  1030. public function timestamp($column, $precision = 0)
  1031. {
  1032. return $this->addColumn('timestamp', $column, compact('precision'));
  1033. }
  1034. /**
  1035. * Create a new timestamp (with time zone) column on the table.
  1036. *
  1037. * @param string $column
  1038. * @param int|null $precision
  1039. * @return \Illuminate\Database\Schema\ColumnDefinition
  1040. */
  1041. public function timestampTz($column, $precision = 0)
  1042. {
  1043. return $this->addColumn('timestampTz', $column, compact('precision'));
  1044. }
  1045. /**
  1046. * Add nullable creation and update timestamps to the table.
  1047. *
  1048. * @param int|null $precision
  1049. * @return void
  1050. */
  1051. public function timestamps($precision = 0)
  1052. {
  1053. $this->timestamp('created_at', $precision)->nullable();
  1054. $this->timestamp('updated_at', $precision)->nullable();
  1055. }
  1056. /**
  1057. * Add nullable creation and update timestamps to the table.
  1058. *
  1059. * Alias for self::timestamps().
  1060. *
  1061. * @param int|null $precision
  1062. * @return void
  1063. */
  1064. public function nullableTimestamps($precision = 0)
  1065. {
  1066. $this->timestamps($precision);
  1067. }
  1068. /**
  1069. * Add creation and update timestampTz columns to the table.
  1070. *
  1071. * @param int|null $precision
  1072. * @return void
  1073. */
  1074. public function timestampsTz($precision = 0)
  1075. {
  1076. $this->timestampTz('created_at', $precision)->nullable();
  1077. $this->timestampTz('updated_at', $precision)->nullable();
  1078. }
  1079. /**
  1080. * Add creation and update datetime columns to the table.
  1081. *
  1082. * @param int|null $precision
  1083. * @return void
  1084. */
  1085. public function datetimes($precision = 0)
  1086. {
  1087. $this->datetime('created_at', $precision)->nullable();
  1088. $this->datetime('updated_at', $precision)->nullable();
  1089. }
  1090. /**
  1091. * Add a "deleted at" timestamp for the table.
  1092. *
  1093. * @param string $column
  1094. * @param int|null $precision
  1095. * @return \Illuminate\Database\Schema\ColumnDefinition
  1096. */
  1097. public function softDeletes($column = 'deleted_at', $precision = 0)
  1098. {
  1099. return $this->timestamp($column, $precision)->nullable();
  1100. }
  1101. /**
  1102. * Add a "deleted at" timestampTz for the table.
  1103. *
  1104. * @param string $column
  1105. * @param int|null $precision
  1106. * @return \Illuminate\Database\Schema\ColumnDefinition
  1107. */
  1108. public function softDeletesTz($column = 'deleted_at', $precision = 0)
  1109. {
  1110. return $this->timestampTz($column, $precision)->nullable();
  1111. }
  1112. /**
  1113. * Add a "deleted at" datetime column to the table.
  1114. *
  1115. * @param string $column
  1116. * @param int|null $precision
  1117. * @return \Illuminate\Database\Schema\ColumnDefinition
  1118. */
  1119. public function softDeletesDatetime($column = 'deleted_at', $precision = 0)
  1120. {
  1121. return $this->datetime($column, $precision)->nullable();
  1122. }
  1123. /**
  1124. * Create a new year column on the table.
  1125. *
  1126. * @param string $column
  1127. * @return \Illuminate\Database\Schema\ColumnDefinition
  1128. */
  1129. public function year($column)
  1130. {
  1131. return $this->addColumn('year', $column);
  1132. }
  1133. /**
  1134. * Create a new binary column on the table.
  1135. *
  1136. * @param string $column
  1137. * @param int|null $length
  1138. * @param bool $fixed
  1139. * @return \Illuminate\Database\Schema\ColumnDefinition
  1140. */
  1141. public function binary($column, $length = null, $fixed = false)
  1142. {
  1143. return $this->addColumn('binary', $column, compact('length', 'fixed'));
  1144. }
  1145. /**
  1146. * Create a new UUID column on the table.
  1147. *
  1148. * @param string $column
  1149. * @return \Illuminate\Database\Schema\ColumnDefinition
  1150. */
  1151. public function uuid($column = 'uuid')
  1152. {
  1153. return $this->addColumn('uuid', $column);
  1154. }
  1155. /**
  1156. * Create a new UUID column on the table with a foreign key constraint.
  1157. *
  1158. * @param string $column
  1159. * @return \Illuminate\Database\Schema\ForeignIdColumnDefinition
  1160. */
  1161. public function foreignUuid($column)
  1162. {
  1163. return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [
  1164. 'type' => 'uuid',
  1165. 'name' => $column,
  1166. ]));
  1167. }
  1168. /**
  1169. * Create a new ULID column on the table.
  1170. *
  1171. * @param string $column
  1172. * @param int|null $length
  1173. * @return \Illuminate\Database\Schema\ColumnDefinition
  1174. */
  1175. public function ulid($column = 'ulid', $length = 26)
  1176. {
  1177. return $this->char($column, $length);
  1178. }
  1179. /**
  1180. * Create a new ULID column on the table with a foreign key constraint.
  1181. *
  1182. * @param string $column
  1183. * @param int|null $length
  1184. * @return \Illuminate\Database\Schema\ForeignIdColumnDefinition
  1185. */
  1186. public function foreignUlid($column, $length = 26)
  1187. {
  1188. return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [
  1189. 'type' => 'char',
  1190. 'name' => $column,
  1191. 'length' => $length,
  1192. ]));
  1193. }
  1194. /**
  1195. * Create a new IP address column on the table.
  1196. *
  1197. * @param string $column
  1198. * @return \Illuminate\Database\Schema\ColumnDefinition
  1199. */
  1200. public function ipAddress($column = 'ip_address')
  1201. {
  1202. return $this->addColumn('ipAddress', $column);
  1203. }
  1204. /**
  1205. * Create a new MAC address column on the table.
  1206. *
  1207. * @param string $column
  1208. * @return \Illuminate\Database\Schema\ColumnDefinition
  1209. */
  1210. public function macAddress($column = 'mac_address')
  1211. {
  1212. return $this->addColumn('macAddress', $column);
  1213. }
  1214. /**
  1215. * Create a new geometry column on the table.
  1216. *
  1217. * @param string $column
  1218. * @param string|null $subtype
  1219. * @param int $srid
  1220. * @return \Illuminate\Database\Schema\ColumnDefinition
  1221. */
  1222. public function geometry($column, $subtype = null, $srid = 0)
  1223. {
  1224. return $this->addColumn('geometry', $column, compact('subtype', 'srid'));
  1225. }
  1226. /**
  1227. * Create a new geography column on the table.
  1228. *
  1229. * @param string $column
  1230. * @param string|null $subtype
  1231. * @param int $srid
  1232. * @return \Illuminate\Database\Schema\ColumnDefinition
  1233. */
  1234. public function geography($column, $subtype = null, $srid = 4326)
  1235. {
  1236. return $this->addColumn('geography', $column, compact('subtype', 'srid'));
  1237. }
  1238. /**
  1239. * Create a new generated, computed column on the table.
  1240. *
  1241. * @param string $column
  1242. * @param string $expression
  1243. * @return \Illuminate\Database\Schema\ColumnDefinition
  1244. */
  1245. public function computed($column, $expression)
  1246. {
  1247. return $this->addColumn('computed', $column, compact('expression'));
  1248. }
  1249. /**
  1250. * Add the proper columns for a polymorphic table.
  1251. *
  1252. * @param string $name
  1253. * @param string|null $indexName
  1254. * @return void
  1255. */
  1256. public function morphs($name, $indexName = null)
  1257. {
  1258. if (Builder::$defaultMorphKeyType === 'uuid') {
  1259. $this->uuidMorphs($name, $indexName);
  1260. } elseif (Builder::$defaultMorphKeyType === 'ulid') {
  1261. $this->ulidMorphs($name, $indexName);
  1262. } else {
  1263. $this->numericMorphs($name, $indexName);
  1264. }
  1265. }
  1266. /**
  1267. * Add nullable columns for a polymorphic table.
  1268. *
  1269. * @param string $name
  1270. * @param string|null $indexName
  1271. * @return void
  1272. */
  1273. public function nullableMorphs($name, $indexName = null)
  1274. {
  1275. if (Builder::$defaultMorphKeyType === 'uuid') {
  1276. $this->nullableUuidMorphs($name, $indexName);
  1277. } elseif (Builder::$defaultMorphKeyType === 'ulid') {
  1278. $this->nullableUlidMorphs($name, $indexName);
  1279. } else {
  1280. $this->nullableNumericMorphs($name, $indexName);
  1281. }
  1282. }
  1283. /**
  1284. * Add the proper columns for a polymorphic table using numeric IDs (incremental).
  1285. *
  1286. * @param string $name
  1287. * @param string|null $indexName
  1288. * @return void
  1289. */
  1290. public function numericMorphs($name, $indexName = null)
  1291. {
  1292. $this->string("{$name}_type");
  1293. $this->unsignedBigInteger("{$name}_id");
  1294. $this->index(["{$name}_type", "{$name}_id"], $indexName);
  1295. }
  1296. /**
  1297. * Add nullable columns for a polymorphic table using numeric IDs (incremental).
  1298. *
  1299. * @param string $name
  1300. * @param string|null $indexName
  1301. * @return void
  1302. */
  1303. public function nullableNumericMorphs($name, $indexName = null)
  1304. {
  1305. $this->string("{$name}_type")->nullable();
  1306. $this->unsignedBigInteger("{$name}_id")->nullable();
  1307. $this->index(["{$name}_type", "{$name}_id"], $indexName);
  1308. }
  1309. /**
  1310. * Add the proper columns for a polymorphic table using UUIDs.
  1311. *
  1312. * @param string $name
  1313. * @param string|null $indexName
  1314. * @return void
  1315. */
  1316. public function uuidMorphs($name, $indexName = null)
  1317. {
  1318. $this->string("{$name}_type");
  1319. $this->uuid("{$name}_id");
  1320. $this->index(["{$name}_type", "{$name}_id"], $indexName);
  1321. }
  1322. /**
  1323. * Add nullable columns for a polymorphic table using UUIDs.
  1324. *
  1325. * @param string $name
  1326. * @param string|null $indexName
  1327. * @return void
  1328. */
  1329. public function nullableUuidMorphs($name, $indexName = null)
  1330. {
  1331. $this->string("{$name}_type")->nullable();
  1332. $this->uuid("{$name}_id")->nullable();
  1333. $this->index(["{$name}_type", "{$name}_id"], $indexName);
  1334. }
  1335. /**
  1336. * Add the proper columns for a polymorphic table using ULIDs.
  1337. *
  1338. * @param string $name
  1339. * @param string|null $indexName
  1340. * @return void
  1341. */
  1342. public function ulidMorphs($name, $indexName = null)
  1343. {
  1344. $this->string("{$name}_type");
  1345. $this->ulid("{$name}_id");
  1346. $this->index(["{$name}_type", "{$name}_id"], $indexName);
  1347. }
  1348. /**
  1349. * Add nullable columns for a polymorphic table using ULIDs.
  1350. *
  1351. * @param string $name
  1352. * @param string|null $indexName
  1353. * @return void
  1354. */
  1355. public function nullableUlidMorphs($name, $indexName = null)
  1356. {
  1357. $this->string("{$name}_type")->nullable();
  1358. $this->ulid("{$name}_id")->nullable();
  1359. $this->index(["{$name}_type", "{$name}_id"], $indexName);
  1360. }
  1361. /**
  1362. * Adds the `remember_token` column to the table.
  1363. *
  1364. * @return \Illuminate\Database\Schema\ColumnDefinition
  1365. */
  1366. public function rememberToken()
  1367. {
  1368. return $this->string('remember_token', 100)->nullable();
  1369. }
  1370. /**
  1371. * Add a comment to the table.
  1372. *
  1373. * @param string $comment
  1374. * @return \Illuminate\Support\Fluent
  1375. */
  1376. public function comment($comment)
  1377. {
  1378. return $this->addCommand('tableComment', compact('comment'));
  1379. }
  1380. /**
  1381. * Add a new index command to the blueprint.
  1382. *
  1383. * @param string $type
  1384. * @param string|array $columns
  1385. * @param string $index
  1386. * @param string|null $algorithm
  1387. * @return \Illuminate\Support\Fluent
  1388. */
  1389. protected function indexCommand($type, $columns, $index, $algorithm = null)
  1390. {
  1391. $columns = (array) $columns;
  1392. // If no name was specified for this index, we will create one using a basic
  1393. // convention of the table name, followed by the columns, followed by an
  1394. // index type, such as primary or index, which makes the index unique.
  1395. $index = $index ?: $this->createIndexName($type, $columns);
  1396. return $this->addCommand(
  1397. $type, compact('index', 'columns', 'algorithm')
  1398. );
  1399. }
  1400. /**
  1401. * Create a new drop index command on the blueprint.
  1402. *
  1403. * @param string $command
  1404. * @param string $type
  1405. * @param string|array $index
  1406. * @return \Illuminate\Support\Fluent
  1407. */
  1408. protected function dropIndexCommand($command, $type, $index)
  1409. {
  1410. $columns = [];
  1411. // If the given "index" is actually an array of columns, the developer means
  1412. // to drop an index merely by specifying the columns involved without the
  1413. // conventional name, so we will build the index name from the columns.
  1414. if (is_array($index)) {
  1415. $index = $this->createIndexName($type, $columns = $index);
  1416. }
  1417. return $this->indexCommand($command, $columns, $index);
  1418. }
  1419. /**
  1420. * Create a default index name for the table.
  1421. *
  1422. * @param string $type
  1423. * @param array $columns
  1424. * @return string
  1425. */
  1426. protected function createIndexName($type, array $columns)
  1427. {
  1428. $table = str_contains($this->table, '.')
  1429. ? substr_replace($this->table, '.'.$this->prefix, strrpos($this->table, '.'), 1)
  1430. : $this->prefix.$this->table;
  1431. $index = strtolower($table.'_'.implode('_', $columns).'_'.$type);
  1432. return str_replace(['-', '.'], '_', $index);
  1433. }
  1434. /**
  1435. * Add a new column to the blueprint.
  1436. *
  1437. * @param string $type
  1438. * @param string $name
  1439. * @param array $parameters
  1440. * @return \Illuminate\Database\Schema\ColumnDefinition
  1441. */
  1442. public function addColumn($type, $name, array $parameters = [])
  1443. {
  1444. return $this->addColumnDefinition(new ColumnDefinition(
  1445. array_merge(compact('type', 'name'), $parameters)
  1446. ));
  1447. }
  1448. /**
  1449. * Add a new column definition to the blueprint.
  1450. *
  1451. * @param \Illuminate\Database\Schema\ColumnDefinition $definition
  1452. * @return \Illuminate\Database\Schema\ColumnDefinition
  1453. */
  1454. protected function addColumnDefinition($definition)
  1455. {
  1456. $this->columns[] = $definition;
  1457. if ($this->after) {
  1458. $definition->after($this->after);
  1459. $this->after = $definition->name;
  1460. }
  1461. return $definition;
  1462. }
  1463. /**
  1464. * Add the columns from the callback after the given column.
  1465. *
  1466. * @param string $column
  1467. * @param \Closure $callback
  1468. * @return void
  1469. */
  1470. public function after($column, Closure $callback)
  1471. {
  1472. $this->after = $column;
  1473. $callback($this);
  1474. $this->after = null;
  1475. }
  1476. /**
  1477. * Remove a column from the schema blueprint.
  1478. *
  1479. * @param string $name
  1480. * @return $this
  1481. */
  1482. public function removeColumn($name)
  1483. {
  1484. $this->columns = array_values(array_filter($this->columns, function ($c) use ($name) {
  1485. return $c['name'] != $name;
  1486. }));
  1487. return $this;
  1488. }
  1489. /**
  1490. * Add a new command to the blueprint.
  1491. *
  1492. * @param string $name
  1493. * @param array $parameters
  1494. * @return \Illuminate\Support\Fluent
  1495. */
  1496. protected function addCommand($name, array $parameters = [])
  1497. {
  1498. $this->commands[] = $command = $this->createCommand($name, $parameters);
  1499. return $command;
  1500. }
  1501. /**
  1502. * Create a new Fluent command.
  1503. *
  1504. * @param string $name
  1505. * @param array $parameters
  1506. * @return \Illuminate\Support\Fluent
  1507. */
  1508. protected function createCommand($name, array $parameters = [])
  1509. {
  1510. return new Fluent(array_merge(compact('name'), $parameters));
  1511. }
  1512. /**
  1513. * Get the table the blueprint describes.
  1514. *
  1515. * @return string
  1516. */
  1517. public function getTable()
  1518. {
  1519. return $this->table;
  1520. }
  1521. /**
  1522. * Get the table prefix.
  1523. *
  1524. * @return string
  1525. */
  1526. public function getPrefix()
  1527. {
  1528. return $this->prefix;
  1529. }
  1530. /**
  1531. * Get the columns on the blueprint.
  1532. *
  1533. * @return \Illuminate\Database\Schema\ColumnDefinition[]
  1534. */
  1535. public function getColumns()
  1536. {
  1537. return $this->columns;
  1538. }
  1539. /**
  1540. * Get the commands on the blueprint.
  1541. *
  1542. * @return \Illuminate\Support\Fluent[]
  1543. */
  1544. public function getCommands()
  1545. {
  1546. return $this->commands;
  1547. }
  1548. /**
  1549. * Get the columns on the blueprint that should be added.
  1550. *
  1551. * @return \Illuminate\Database\Schema\ColumnDefinition[]
  1552. */
  1553. public function getAddedColumns()
  1554. {
  1555. return array_filter($this->columns, function ($column) {
  1556. return ! $column->change;
  1557. });
  1558. }
  1559. /**
  1560. * Get the columns on the blueprint that should be changed.
  1561. *
  1562. * @return \Illuminate\Database\Schema\ColumnDefinition[]
  1563. */
  1564. public function getChangedColumns()
  1565. {
  1566. return array_filter($this->columns, function ($column) {
  1567. return (bool) $column->change;
  1568. });
  1569. }
  1570. }