【问题标题】:Passing an element of a collection in the query builder在查询构建器中传递集合的元素
【发布时间】: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


【解决方案1】:

目前您将整个组织对象传递到 where 子句中,但您只需要 id

要访问id 属性,您可以使用$organization->id

DB::table('follow_organizations')->where('organization_id', $organization->id)->count();

Setting and Getting Property Values

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-16
    • 2014-01-27
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2018-10-27
    • 1970-01-01
    相关资源
    最近更新 更多