【发布时间】:2019-08-06 13:18:44
【问题描述】:
我想用上一个请求中获得的 id 发出第二个请求。
我有 3 张桌子:
- 组织
- 用户
- follow_organization(关系表)
我想通过传递之前获取的组织的id来获取follow_organization表中出现的次数。
$organization = DB::table('organizations')->where('name', $name)->first();
$followers = DB::table('follow_organizations')->where('organization_id', $organization)->count();
很遗憾,我收到以下错误消息:
stdClass类的对象无法转换为字符串。
我理解了这个问题,我在查询生成器等待字符串时传递了一个表。 但是,尽管我在互联网上阅读了大量答案,但我找不到解决这个问题的方法。
【问题讨论】:
-
您需要将
id从组织传递到where子句,此时您正在传递整个对象:DB::table('follow_organizations')->where('organization_id', $organization->id)->count(); -
感谢@Remul!我没有解决方案只传递 id 而不是整个集合!我不能将您的评论放在“解决方案”中,但这是正确的解决方案
标签: laravel-5 eloquent laravel-query-builder