Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with constrained() method used after foreignIdFor(), instead of table name when $table parameter is not passed uses column name #53144

Merged
merged 9 commits into from
Oct 15, 2024
11 changes: 7 additions & 4 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,15 +1011,17 @@ public function unsignedBigInteger($column, $autoIncrement = false)
* Create a new unsigned big integer (8-byte) column on the table.
*
* @param string $column
* @param string $table
* @return \Illuminate\Database\Schema\ForeignIdColumnDefinition
*/
public function foreignId($column)
public function foreignId($column, $table = null)
{
return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [
'type' => 'bigInteger',
'name' => $column,
'autoIncrement' => false,
'unsigned' => true,
'table' => $table,
]));
}

Expand All @@ -1039,7 +1041,7 @@ public function foreignIdFor($model, $column = null)
$column = $column ?: $model->getForeignKey();

if ($model->getKeyType() === 'int' && $model->getIncrementing()) {
return $this->foreignId($column);
return $this->foreignId($column, $model->getTable());
}

$modelTraits = class_uses_recursive($model);
Expand All @@ -1048,7 +1050,7 @@ public function foreignIdFor($model, $column = null)
return $this->foreignUlid($column);
}

return $this->foreignUuid($column);
return $this->foreignUuid($column, $model->getTable());
}

/**
Expand Down Expand Up @@ -1356,11 +1358,12 @@ public function uuid($column = 'uuid')
* @param string $column
* @return \Illuminate\Database\Schema\ForeignIdColumnDefinition
*/
public function foreignUuid($column)
public function foreignUuid($column, $table = null)
{
return $this->addColumnDefinition(new ForeignIdColumnDefinition($this, [
'type' => 'uuid',
'name' => $column,
'table' => $table,
]));
}

Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Schema/ForeignIdColumnDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public function __construct(Blueprint $blueprint, $attributes = [])
*/
public function constrained($table = null, $column = 'id', $indexName = null)
{
if (is_null($table) && is_null($this->table)) {
granitibrahimi marked this conversation as resolved.
Show resolved Hide resolved
$table = $this->table;
}

return $this->references($column, $indexName)->on($table ?? Str::of($this->name)->beforeLast('_'.$column)->plural());
}

Expand Down