php - laravel - foreignId() 和 unsignedBigInteger() 之间的区别

标签 php database laravel

Laravel 新手

链接表时foreignId()和unsignedBigInteger()有什么区别

$table->unsignedBigInteger('user_id');
$table->foreignId('user_id');

我已经尝试了两者,它们都有效。

根据documentation它说:

The foreignId method is an alias for unsignedBigInteger



但什么是 别名 意思?这是否意味着它们是相同的?

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/

相关文章:

php - 解析错误,需要 `"标识符(T_STRING )"' 或 `"\\(T_NS_SEPARATOR )"' in EG_14/Starter/cart_additem.php on line 4

php - Laravel 如何在创建 7 天后删除数据库记录

php - Laravel 5 MethodNotAllowedHttpException PUT

php - 使用 Resource 显示数据后分页不显示

php - 如何在不使用 J 安装管理器的情况下通过复制文件来安装 Joomla 2.5 组件

php - 如何在 macOS 上安装 Composer?

PHP/mysql 未按预期检索

database - 使用 Perl 访问 BerkeleyDB 的正确方法是什么?

java - 关于如何制作与 SQL 数据库交互的 Java Web 应用程序的粗略概述?

mysql - 频繁出现#126 - Incorrect key file for table '/tmp/#sql_4a05_0.MYI' 的解决方案是什么;尝试在mysql中修复它?