Skip to content

Commit

Permalink
[10.x] Make the Schema Builder macroable (#49547)
Browse files Browse the repository at this point in the history
* make the schema builder macroable

* stop passing the  parameter to the closure.
  • Loading branch information
kevinb1989 authored Jan 3, 2024
1 parent 016ae04 commit fa446dc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
use Closure;
use Illuminate\Container\Container;
use Illuminate\Database\Connection;
use Illuminate\Support\Traits\Macroable;
use InvalidArgumentException;
use LogicException;

class Builder
{
use Macroable;

/**
* The database connection instance.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Integration/Database/SchemaBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,33 @@ public function testSystemVersionedTables()

DB::statement('create table `test` (`foo` int) WITH system versioning;');
}

public function testAddingMacros()
{
Schema::macro('foo', fn () => 'foo');

$this->assertEquals('foo', Schema::foo());

Schema::macro('hasForeignKeyForColumn', function (string $column, string $table, string $foreignTable) {
return collect(Schema::getForeignKeys($table))
->contains(function (array $foreignKey) use ($column, $foreignTable) {
return collect($foreignKey['columns'])->contains($column)
&& $foreignKey['foreign_table'] == $foreignTable;
});
});

Schema::create('questions', function (Blueprint $table) {
$table->id();
$table->string('body');
});

Schema::create('answers', function (Blueprint $table) {
$table->id();
$table->string('body');
$table->foreignId('question_id')->constrained();
});

$this->assertTrue(Schema::hasForeignKeyForColumn('question_id', 'answers', 'questions'));
$this->assertFalse(Schema::hasForeignKeyForColumn('body', 'answers', 'questions'));
}
}

0 comments on commit fa446dc

Please sign in to comment.