Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Added whenTableHasColumn and whenTableDoesntHaveColumn on Schema Builder #41517

Merged
merged 2 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Support/Facades/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down