【发布时间】:2021-12-19 17:16:15
【问题描述】:
我使用 uuid 作为帐户表的主键,它与角色表有多对多的关系。由于我在 mariadb 中创建了一个名为 UuidToBin 的函数,我将 uuid 以二进制(16)的形式存储在数据库中,将 uuid 字符串转换为二进制(16),当我想要获取 uuid 时,我使用另一个函数名为 UuidFromBin 将 uuid 从二进制 (16) 转换为字符串,请参阅本文:https://docs.w3cub.com/mariadb/guiduuid-performance/index。问题是当我使用 laravel eloquent 关系时我无法检索数据,因为它自动传递帐户表的主键的值以将查询与角色表连接起来,而不是首先调用将 uuid 转换为二进制(16)的函数,以便 mariadb 可以比较。 代码关系:
**
public function roles () : BelongsToMany
{
return $this->belongsToMany(Role::class, 'account_role',
'uuid_account', 'id_role');
}
** 查询 laravel 执行:
select `role`.`id` from `role` inner join `account_role` on `role`.`id` = `account_role`.`id_role`
where `account_role`.`uuid_account` = '94cb6c01-3c6d-44fe-b40f-e798bb25d972'
我想要的查询:
select `role`.`id` from `role` inner join `account_role` on `role`.`id` = `account_role`.`id_role`
where `account_role`.`uuid_account` = UuidToBin('94cb6c01-3c6d-44fe-b40f-e798bb25d972')
如果有人知道如何修复,或者如果有更好的方法来使用 uuid,请告诉。非常感谢!
【问题讨论】:
-
Laravel 没有办法调用“函数”吗?
-
当你在 laravel orm 中调用关系时,它会自动执行上面的第一个查询。我无法修改它以通过 DB::raw() 或类似的方式将函数传递给它。如果你知道怎么做,请告诉我
-
<frustration>有一百种 SQL 抽象产品(如 Laravel)尝试向用户隐藏 SQL。用户最终会出现在这样的论坛中,询问如何做某事或如何加快 SQL 的速度。通常的答案是学习 MySQL 除了抽象 并使用::raw.</frustration>
标签: php mysql laravel orm mariadb