Skip to content

Commit fc75acc

Browse files
authored
[9.x] Added whenTableHasColumn and whenTableHasNotColumn on Schema Builder
1 parent 3dd2a32 commit fc75acc

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/Illuminate/Database/Schema/Builder.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,36 @@ public function hasColumns($table, array $columns)
170170
return true;
171171
}
172172

173+
/**
174+
* Execute a callback inside table builder if has a column
175+
*
176+
* @param string $table
177+
* @param string $column
178+
* @param \Closure $callback
179+
* @return void
180+
*/
181+
public function whenTableHasColumn(string $table, string $column, Closure $callback): void
182+
{
183+
if ($this->hasColumn($table, $column)) {
184+
$this->table($table, fn (Blueprint $table) => $callback($table));
185+
}
186+
}
187+
188+
/**
189+
* Execute a callback inside table builder if does't have a column
190+
*
191+
* @param string $table
192+
* @param string $column
193+
* @param \Closure $callback
194+
* @return void
195+
*/
196+
public function whenTableHasNotColumn(string $table, string $column, Closure $callback): void
197+
{
198+
if (! $this->hasColumn($table, $column)) {
199+
$this->table($table, fn (Blueprint $table) => $callback($table));
200+
}
201+
}
202+
173203
/**
174204
* Get the data type for the given column name.
175205
*

0 commit comments

Comments
 (0)