【发布时间】:2021-06-01 22:42:00
【问题描述】:
我有两张桌子
订阅表
id char(36) | other column
许可证表
id bigint(20) | license_key (string) | subscription_id char(36)
模型订阅.php
protected $with = ['licenses'];
public function licenses()
{
return $this->hasMany('App\License', 'subscription_id');
}
Eager Loading 适用于大多数情况,但 subscription_id = 6e20af64-81e6-428e-b9db-52317d1e478e
subscription_id = 6e20af64-81e6-428e-b9db-52317d1e478e 存在于许可证表中
这是查询日志:
array:2 [▼
0 => array:3 [▼
"query" => "select * from `subscriptions` where `subscriptions`.`id` = ? limit 1"
"bindings" => array:1 [▼
0 => "6e20af64-81e6-428e-b9db-52317d1e478e"
]
"time" => 3.9
]
1 => array:3 [▼
"query" => "select * from `licenses` where `licenses`.`subscription_id` in (9223372036854775807)"
"bindings" => []
"time" => 4.73
]
]
为什么它不能只使用这个 subscription_id?
我的环境:
"laravel/framework": "6.20.17",
php: 7.4.14
【问题讨论】:
-
你试过了吗:return $this->hasMany(License::class, 'foreign_key', 'local_key'); ?
-
正如它所说,
// In Laravel 6.0+ make sure to also set $keyType protected $keyType = 'string';
标签: php laravel eager-loading