Skip to content

Commit fa446dc

Browse files
authored
[10.x] Make the Schema Builder macroable (#49547)
* make the schema builder macroable * stop passing the parameter to the closure.
1 parent 016ae04 commit fa446dc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Illuminate/Database/Schema/Builder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
use Closure;
66
use Illuminate\Container\Container;
77
use Illuminate\Database\Connection;
8+
use Illuminate\Support\Traits\Macroable;
89
use InvalidArgumentException;
910
use LogicException;
1011

1112
class Builder
1213
{
14+
use Macroable;
15+
1316
/**
1417
* The database connection instance.
1518
*

tests/Integration/Database/SchemaBuilderTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,33 @@ public function testSystemVersionedTables()
384384

385385
DB::statement('create table `test` (`foo` int) WITH system versioning;');
386386
}
387+
388+
public function testAddingMacros()
389+
{
390+
Schema::macro('foo', fn () => 'foo');
391+
392+
$this->assertEquals('foo', Schema::foo());
393+
394+
Schema::macro('hasForeignKeyForColumn', function (string $column, string $table, string $foreignTable) {
395+
return collect(Schema::getForeignKeys($table))
396+
->contains(function (array $foreignKey) use ($column, $foreignTable) {
397+
return collect($foreignKey['columns'])->contains($column)
398+
&& $foreignKey['foreign_table'] == $foreignTable;
399+
});
400+
});
401+
402+
Schema::create('questions', function (Blueprint $table) {
403+
$table->id();
404+
$table->string('body');
405+
});
406+
407+
Schema::create('answers', function (Blueprint $table) {
408+
$table->id();
409+
$table->string('body');
410+
$table->foreignId('question_id')->constrained();
411+
});
412+
413+
$this->assertTrue(Schema::hasForeignKeyForColumn('question_id', 'answers', 'questions'));
414+
$this->assertFalse(Schema::hasForeignKeyForColumn('body', 'answers', 'questions'));
415+
}
387416
}

0 commit comments

Comments
 (0)