好吧,这是我的迁移......
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 的存储方式。我怎样才能让这种关系发挥作用?