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
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -1039,16 +1039,16 @@ public function foreignIdFor($model, $column = null)
$column = $column ?: $model->getForeignKey();

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

$modelTraits = class_uses_recursive($model);

if (in_array(HasUlids::class, $modelTraits, true)) {
return $this->foreignUlid($column);
return $this->foreignUlid($column, 26)->table($model->getTable());
}

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

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Schema/ForeignIdColumnDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function __construct(Blueprint $blueprint, $attributes = [])
*/
public function constrained($table = null, $column = 'id', $indexName = null)
{
$table ??= $this->table;

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

Expand Down