diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index dd4290aa3f6e..d4c1afec30cb 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -170,6 +170,36 @@ public function hasColumns($table, array $columns) return true; } + /** + * Execute a callback inside table builder if has a column + * + * @param string $table + * @param string $column + * @param \Closure $callback + * @return void + */ + public function whenTableHasColumn(string $table, string $column, Closure $callback): void + { + if ($this->hasColumn($table, $column)) { + $this->table($table, fn (Blueprint $table) => $callback($table)); + } + } + + /** + * Execute a callback inside table builder if does't have a column + * + * @param string $table + * @param string $column + * @param \Closure $callback + * @return void + */ + public function whenTableHasNotColumn(string $table, string $column, Closure $callback): void + { + if (! $this->hasColumn($table, $column)) { + $this->table($table, fn (Blueprint $table) => $callback($table)); + } + } + /** * Get the data type for the given column name. * diff --git a/src/Illuminate/Support/Facades/Schema.php b/src/Illuminate/Support/Facades/Schema.php index 2b0584d3fa5f..97b68e924473 100755 --- a/src/Illuminate/Support/Facades/Schema.php +++ b/src/Illuminate/Support/Facades/Schema.php @@ -15,6 +15,8 @@ * @method static bool hasColumn(string $table, string $column) * @method static bool hasColumns(string $table, array $columns) * @method static bool dropColumns(string $table, array $columns) + * @method static void whenTableHasColumn(string $table, string $column, \Closure $callback) + * @method static void whenTableNotHasColumn(string $table, string $column, \Closure $callback) * @method static bool hasTable(string $table) * @method static void defaultStringLength(int $length) * @method static array getColumnListing(string $table)