在经典的 Rest API 方法中,我的应用程序需要检查用户是否与某个帐户相关。我们可以对多个帐户执行操作,因此每个请求都会执行查询。 由于这是我们最常调用的查询,因此它的轻便性对于我安心来说非常重要。
当前查询,使用 Eloquent
$user-> accounts()-> where($primaryKey, $id);
不知何故,这两个都会产生错误(Eloquent bug?)
$user-> accounts()-> find ($id);
$user-> accounts()-> where ($primaryKey, $id)->count();
错误:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'a_id' in where clause is ambiguous (SQL: select * from 'accounts' inner join 'users_accounts' on 'accounts'.'a_id' = 'users_accounts'.'a_id' where 'users_accounts'.'u_id' = 1 and 'a_id' = 1 limit 1)
回到我的问题,有没有比“where”函数更优雅的解决方案?
最佳答案
如果这是您最常用的查询之一,为什么不缓存它?
$user->accounts()->where($primaryKey, '=', $id)->remember(60)->get();
上述查询失败,因为两个表中都有同名的列
where 'users_accounts'.'u_id' = 1 and 'a_id' = 1
关于php - Laravel 对关系最轻查询的 Eloquent 搜索是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26095307/