|
9 | 9 | use Illuminate\Database\Schema\Grammars\Grammar;
|
10 | 10 | use Illuminate\Database\Schema\Grammars\MySqlGrammar;
|
11 | 11 | use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
|
| 12 | +use Illuminate\Support\Facades\Schema; |
12 | 13 | use Illuminate\Support\Fluent;
|
13 | 14 | use Illuminate\Support\Traits\Macroable;
|
14 | 15 |
|
@@ -427,6 +428,25 @@ public function dropColumn($columns)
|
427 | 428 | return $this->addCommand('dropColumn', compact('columns'));
|
428 | 429 | }
|
429 | 430 |
|
| 431 | + /** |
| 432 | + * Indicate that the given columns should be dropped if they exist. |
| 433 | + * |
| 434 | + * @param array|mixed $columns |
| 435 | + * @return \Illuminate\Support\Fluent |
| 436 | + */ |
| 437 | + public function dropColumnIfExists($columns) |
| 438 | + { |
| 439 | + $columns = is_array($columns) ? $columns : func_get_args(); |
| 440 | + |
| 441 | + $columns = array_intersect($columns, Schema::getColumnListing($this->getTable())); |
| 442 | + |
| 443 | + if (empty($columns)) { |
| 444 | + return new Fluent; |
| 445 | + } |
| 446 | + |
| 447 | + return $this->dropColumn($columns); |
| 448 | + } |
| 449 | + |
430 | 450 | /**
|
431 | 451 | * Indicate that the given columns should be renamed.
|
432 | 452 | *
|
@@ -505,6 +525,25 @@ public function dropForeign($index)
|
505 | 525 | return $this->dropIndexCommand('dropForeign', 'foreign', $index);
|
506 | 526 | }
|
507 | 527 |
|
| 528 | + /** |
| 529 | + * Indicate that the given foreign key should be dropped if it exists. |
| 530 | + * |
| 531 | + * @param string|array $index |
| 532 | + * @return \Illuminate\Support\Fluent |
| 533 | + */ |
| 534 | + public function dropForeignIfExists($index) |
| 535 | + { |
| 536 | + if (is_array($index)) { |
| 537 | + $index = $this->createIndexName('foreign', $index); |
| 538 | + } |
| 539 | + |
| 540 | + if (! in_array($index, Schema::getForeignKeys($this->getTable()))) { |
| 541 | + return new Fluent; |
| 542 | + } |
| 543 | + |
| 544 | + return $this->dropIndexCommand('dropForeign', 'foreign', $index); |
| 545 | + } |
| 546 | + |
508 | 547 | /**
|
509 | 548 | * Indicate that the given column and foreign key should be dropped.
|
510 | 549 | *
|
|
0 commit comments