From 12fc342c0afb55b3701af4ac447394406814bc02 Mon Sep 17 00:00:00 2001 From: Kevin Bui Date: Tue, 2 Jan 2024 19:02:09 +1100 Subject: [PATCH 1/2] make the schema builder macroable --- src/Illuminate/Database/Schema/Builder.php | 3 ++ .../Database/SchemaBuilderTest.php | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index c4b3da940265..770c6c52655c 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -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. * diff --git a/tests/Integration/Database/SchemaBuilderTest.php b/tests/Integration/Database/SchemaBuilderTest.php index 7f3c8546881f..d00397e021a3 100644 --- a/tests/Integration/Database/SchemaBuilderTest.php +++ b/tests/Integration/Database/SchemaBuilderTest.php @@ -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, $table, $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')); + } } From 43175b94619a00d2f5d9938e95d06b3939a1d497 Mon Sep 17 00:00:00 2001 From: Kevin Bui Date: Tue, 2 Jan 2024 19:36:14 +1100 Subject: [PATCH 2/2] stop passing the parameter to the closure. --- tests/Integration/Database/SchemaBuilderTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Database/SchemaBuilderTest.php b/tests/Integration/Database/SchemaBuilderTest.php index d00397e021a3..fbd42e13c489 100644 --- a/tests/Integration/Database/SchemaBuilderTest.php +++ b/tests/Integration/Database/SchemaBuilderTest.php @@ -393,7 +393,7 @@ public function testAddingMacros() Schema::macro('hasForeignKeyForColumn', function (string $column, string $table, string $foreignTable) { return collect(Schema::getForeignKeys($table)) - ->contains(function (array $foreignKey) use ($column, $table, $foreignTable) { + ->contains(function (array $foreignKey) use ($column, $foreignTable) { return collect($foreignKey['columns'])->contains($column) && $foreignKey['foreign_table'] == $foreignTable; });