Skip to content

Commit 715f60e

Browse files
authored
Add whenTableHasIndex and whenTableDoesntHaveIndex to Builder (#58005)
* Update Builder.php * apply big T feedback * fix cs
1 parent 6567d33 commit 715f60e

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Illuminate/Database/Schema/Builder.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,38 @@ public function whenTableDoesntHaveColumn(string $table, string $column, Closure
322322
}
323323
}
324324

325+
/**
326+
* Execute a table builder callback if the given table has a given index.
327+
*
328+
* @param string $table
329+
* @param string|array $index
330+
* @param \Closure $callback
331+
* @param string|null $type
332+
* @return void
333+
*/
334+
public function whenTableHasIndex(string $table, string|array $index, Closure $callback, ?string $type = null)
335+
{
336+
if ($this->hasIndex($table, $index, $type)) {
337+
$this->table($table, fn (Blueprint $table) => $callback($table));
338+
}
339+
}
340+
341+
/**
342+
* Execute a table builder callback if the given table doesn't have a given index.
343+
*
344+
* @param string $table
345+
* @param string|array $index
346+
* @param \Closure $callback
347+
* @param string|null $type
348+
* @return void
349+
*/
350+
public function whenTableDoesntHaveIndex(string $table, string|array $index, Closure $callback, ?string $type = null)
351+
{
352+
if (! $this->hasIndex($table, $index, $type)) {
353+
$this->table($table, fn (Blueprint $table) => $callback($table));
354+
}
355+
}
356+
325357
/**
326358
* Get the data type for the given column name.
327359
*

0 commit comments

Comments
 (0)