Skip to content

Commit b6e478d

Browse files
eusonlitoMrPunyapaltaylorotwell
authored
[11.x] Added dropColumnsIfExists, dropColumnIfExists and dropForeignIfExists (#53305)
* Added dropColumnsIfExists and dropColumnIfExists * Update src/Illuminate/Database/Schema/Blueprint.php Thanks! Co-authored-by: Punyapal Shah <[email protected]> * Simplified dropColumnIfExists * Added `dropForeignIfExists` to complement `dropColumnIfExists` * Updated dropForeignIfExists comment * Update Blueprint.php * Update Blueprint.php * Update Blueprint.php * Update Builder.php --------- Co-authored-by: Punyapal Shah <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent d4aa229 commit b6e478d

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/Illuminate/Database/Schema/Blueprint.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Schema\Grammars\Grammar;
1010
use Illuminate\Database\Schema\Grammars\MySqlGrammar;
1111
use Illuminate\Database\Schema\Grammars\SQLiteGrammar;
12+
use Illuminate\Support\Facades\Schema;
1213
use Illuminate\Support\Fluent;
1314
use Illuminate\Support\Traits\Macroable;
1415

@@ -427,6 +428,25 @@ public function dropColumn($columns)
427428
return $this->addCommand('dropColumn', compact('columns'));
428429
}
429430

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+
430450
/**
431451
* Indicate that the given columns should be renamed.
432452
*
@@ -505,6 +525,25 @@ public function dropForeign($index)
505525
return $this->dropIndexCommand('dropForeign', 'foreign', $index);
506526
}
507527

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+
508547
/**
509548
* Indicate that the given column and foreign key should be dropped.
510549
*

src/Illuminate/Database/Schema/Builder.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,20 @@ public function dropColumns($table, $columns)
462462
});
463463
}
464464

465+
/**
466+
* Drop columns from a table schema if they exist.
467+
*
468+
* @param string $table
469+
* @param string|array $columns
470+
* @return void
471+
*/
472+
public function dropColumnsIfExists($table, $columns)
473+
{
474+
$this->table($table, function (Blueprint $blueprint) use ($columns) {
475+
$blueprint->dropColumnIfExists($columns);
476+
});
477+
}
478+
465479
/**
466480
* Drop all tables from the database.
467481
*

0 commit comments

Comments
 (0)