Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

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