Php 关系中将Laravel bigInteger四舍五入为int
好的,这是我的迁移Php 关系中将Laravel bigInteger四舍五入为int,php,laravel,eloquent,Php,Laravel,Eloquent,好的,这是我的迁移 public function up() { Schema::create('instagrams', function (Blueprint $table) { $table->bigInteger('id')->unsigned()->primary(); // ... }); } public function up() { Schema::create('users', function (
public function up()
{
Schema::create('instagrams', function (Blueprint $table) {
$table->bigInteger('id')->unsigned()->primary();
// ...
});
}
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('instagram_id')->unsigned()->nullable();
// ...
});
}
我有一个用户模型和一个instagram模型。这是我的instagram模型:
class Instagram extends Model
{
public function user()
{
return $this->hasOne('App\User');
}
}
我的问题是instagram与用户的关系不正常。我无法从instagram访问用户,即使他们都在数据库中
>>> $u = App\User::first()
=> App\User {#695
id: 1,
instagram_id: "3620243170",
}
>>> $i = App\Instagram::first()
=> App\Instagram {#696
id: "3620243170",
}
>>> $i->user
=> null
所以,我花了很长时间绞尽脑汁,直到我找到了这些有用的修补方法。。。下面是它给我的信息:
>>> $i->user()->toSql()
=> "select * from `users` where `users`.`instagram_id` = ? and `users`.`instagram_id` is not null"
>>> $i->user()->getBindings()
=> [
2147483647,
]
一切正常,除了ID被隐藏在laravel中的任何代码在32位限制下最大化。。。ID必须大于32位,因为instagram的ID就是这样存储的。如何使这种关系起作用?听起来您使用的是32位版本的PHP,其中最大整数值为2147483647 问题是,当关系查询获取
Instagram
实例的键值以查询用户时,它会自动将该id值转换为模型上$keyType
属性定义的类型。默认情况下,此属性为int
因此,即使您的Instagram
实例id是“3620243170”
,它也会被转换为int,在32位PHP中会转换为2147483647
您可以尝试以下几点来缓解此问题:
Instagram
机型上的$keyType
属性更改为float
,或者可能将字符串更改为。这只会影响Eloquent在PHP中对变量的转换,而不会影响它们在数据库中的存储方式。
添加protected$keyType='float'代码>到您的Instagram
型号
听起来您使用的是32位版本的PHP,其中最大整数值为2147483647 问题是,当关系查询获取
Instagram
实例的键值以查询用户时,它会自动将该id值转换为模型上$keyType
属性定义的类型。默认情况下,此属性为int
因此,即使您的Instagram
实例id是“3620243170”
,它也会被转换为int,在32位PHP中会转换为2147483647
您可以尝试以下几点来缓解此问题:
Instagram
机型上的$keyType
属性更改为float
,或者可能将字符串更改为。这只会影响Eloquent在PHP中对变量的转换,而不会影响它们在数据库中的存储方式。
添加protected$keyType='float'代码>到您的Instagram
型号
将Instagram ID存储为字符串而不是大整数。如果将
Instagram
模型上的$keyType
属性设置为'float'
,是否有效?即:protected$keyType='float'
@sisve可以工作,但感觉像是解决了一个不必要的问题…将Instagram ID存储为字符串而不是大整数。如果您将Instagram
模型上的$keyType
属性设置为'float'
,是否可以工作?即:protected$keyType='float'代码>@sisve可以,但感觉像是解决了一个不必要的问题…非常感谢!我选择更改键类型,而不是依赖于特定的PHP环境,因为我可能无法始终控制它。非常感谢!我选择更改键类型,而不是依赖某个PHP环境,因为我可能并不总是能够控制它。