Laravel 新手
链接表时foreignId()和unsignedBigInteger()有什么区别
$table->unsignedBigInteger('user_id');
$table->foreignId('user_id');
我已经尝试了两者,它们都有效。
根据documentation它说:
The
foreignId
method is an alias forunsignedBigInteger
但什么是 别名 意思?这是否意味着它们是相同的?
PS:
我没有使用文档中的代码,而只是
$table->unsignedBigInteger('user_id');
和/或
$table->foreignId('user_id');
最佳答案
如果您查看 Blueprint.php,您将看到两种方法:
/**
* Create a new unsigned big integer (8-byte) column on the table.
*
* @param string $column
* @param bool $autoIncrement
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function unsignedBigInteger($column, $autoIncrement = false)
{
return $this->bigInteger($column, $autoIncrement, true);
}
/**
* Create a new unsigned big integer (8-byte) column on the table.
*
* @param string $column
* @return \Illuminate\Database\Schema\ForeignIdColumnDefinition
*/
public function foreignId($column)
{
$this->columns[] = $column = new ForeignIdColumnDefinition($this, [
'type' => 'bigInteger',
'name' => $column,
'autoIncrement' => false,
'unsigned' => true,
]);
return $column;
}
因此,默认情况下它使用“bigInteger”列的类型,“unsigned”设置为true。最后,他们是一样的。
唯一的区别是,使用“unsignedBigInteger”,您可以控制 $autoIncrement 设置为 true 还是 false,而不是 foreignId
关于php - laravel - foreignId() 和 unsignedBigInteger() 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61002912/