Skip to content

Commit 8e0a396

Browse files
committed
Merge remote-tracking branch 'origin/10.x' into 10.x
2 parents e86cb93 + ef77d87 commit 8e0a396

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

src/Illuminate/Database/Schema/ForeignIdColumnDefinition.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,24 @@ public function __construct(Blueprint $blueprint, $attributes = [])
3131
* Create a foreign key constraint on this column referencing the "id" column of the conventionally related table.
3232
*
3333
* @param string|null $table
34-
* @param string $column
34+
* @param string|null $column
35+
* @param string|null $indexName
3536
* @return \Illuminate\Database\Schema\ForeignKeyDefinition
3637
*/
37-
public function constrained($table = null, $column = 'id')
38+
public function constrained($table = null, $column = 'id', $indexName = null)
3839
{
39-
return $this->references($column)->on($table ?? Str::of($this->name)->beforeLast('_'.$column)->plural());
40+
return $this->references($column, $indexName)->on($table ?? Str::of($this->name)->beforeLast('_'.$column)->plural());
4041
}
4142

4243
/**
4344
* Specify which column this foreign ID references on another table.
4445
*
4546
* @param string $column
47+
* @param string $indexName
4648
* @return \Illuminate\Database\Schema\ForeignKeyDefinition
4749
*/
48-
public function references($column)
50+
public function references($column, $indexName = null)
4951
{
50-
return $this->blueprint->foreign($this->name)->references($column);
52+
return $this->blueprint->foreign($this->name, $indexName)->references($column);
5153
}
5254
}

tests/Database/DatabaseMySqlSchemaGrammarTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,17 @@ public function testAddingForeignID()
497497
], $statements);
498498
}
499499

500+
public function testAddingForeignIdSpecifyingIndexNameInConstraint()
501+
{
502+
$blueprint = new Blueprint('users');
503+
$blueprint->foreignId('company_id')->constrained(indexName: 'my_index');
504+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
505+
$this->assertSame([
506+
'alter table `users` add `company_id` bigint unsigned not null',
507+
'alter table `users` add constraint `my_index` foreign key (`company_id`) references `companies` (`id`)',
508+
], $statements);
509+
}
510+
500511
public function testAddingBigIncrementingID()
501512
{
502513
$blueprint = new Blueprint('users');

tests/Database/DatabasePostgresSchemaGrammarTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,17 @@ public function testAddingForeignID()
416416
], $statements);
417417
}
418418

419+
public function testAddingForeignIdSpecifyingIndexNameInConstraint()
420+
{
421+
$blueprint = new Blueprint('users');
422+
$blueprint->foreignId('company_id')->constrained(indexName: 'my_index');
423+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
424+
$this->assertSame([
425+
'alter table "users" add column "company_id" bigint not null',
426+
'alter table "users" add constraint "my_index" foreign key ("company_id") references "companies" ("id")',
427+
], $statements);
428+
}
429+
419430
public function testAddingBigIncrementingID()
420431
{
421432
$blueprint = new Blueprint('users');

tests/Database/DatabaseSQLiteSchemaGrammarTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ public function testAddingForeignID()
321321
], $statements);
322322
}
323323

324+
public function testAddingForeignIdSpecifyingIndexNameInConstraint()
325+
{
326+
$blueprint = new Blueprint('users');
327+
$blueprint->foreignId('company_id')->constrained(indexName: 'my_index');
328+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
329+
$this->assertSame([
330+
'alter table "users" add column "company_id" integer not null',
331+
], $statements);
332+
}
333+
324334
public function testAddingBigIncrementingID()
325335
{
326336
$blueprint = new Blueprint('users');

tests/Database/DatabaseSqlServerSchemaGrammarTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,17 @@ public function testAddingForeignID()
366366
], $statements);
367367
}
368368

369+
public function testAddingForeignIdSpecifyingIndexNameInConstraint()
370+
{
371+
$blueprint = new Blueprint('users');
372+
$blueprint->foreignId('company_id')->constrained(indexName: 'my_index');
373+
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());
374+
$this->assertSame([
375+
'alter table "users" add "company_id" bigint not null',
376+
'alter table "users" add constraint "my_index" foreign key ("company_id") references "companies" ("id")',
377+
], $statements);
378+
}
379+
369380
public function testAddingBigIncrementingID()
370381
{
371382
$blueprint = new Blueprint('users');

0 commit comments

Comments
 (0)