【问题标题】:laravel raw query doesn't return resultslaravel 原始查询不返回结果
【发布时间】:2015-01-08 11:30:06
【问题描述】:

我有下面的代码

$id = 1;
$idz = 3;
$nums = DB::select(DB::raw('select * from chat_user where user_id in (?, ?) and chat_id in (select chat_id from chat_user group by chat_id having count(*) > 1)'), array($id, $idz));
return $nums->count();

这里我有一个名为“user_chat”的表,我想知道我是否通过他们的“user_id”选择了两行,它们是否具有相同的“chat_id”。当我在 phpmyadmin 的 sql 部分测试代码并返回结果时,代码运行良好。

select * from chat_user where user_id in (1, 3) and chat_id in (select chat_id from chat_user group by chat_id having count(*) > 1)

但是当我用 laravel 的风格写它时,我得到了这个错误:

Call to a member function count() on a non-object

任何帮助都会非常棒!

【问题讨论】:

    标签: php mysql laravel laravel-4 query-builder


    【解决方案1】:

    DB::select 返回一个数组,而不是一个对象。

    请改用count($nums)

    【讨论】:

      【解决方案2】:

      当你使用DB::select()时,返回的东西就是结果。不是查询生成器对象。

      您可以通过三种方式解决此问题。

      • 运行另一个SELECT COUNT(id)....
      • 如果您选择所有行,则计算数组中的项目。
      • 将其更改为查询生成器。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 1970-01-01
        • 2021-09-18
        • 1970-01-01
        相关资源
        最近更新 更多