简体   繁体   English

laravel原始查询不返回结果

[英]laravel raw query doesn't return results

i have the code below 我有下面的代码

$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();

here i have a table named "user_chat" and i want to find out if i select two rows by their "user_id" do they have the same "chat_id". 在这里,我有一个名为“ user_chat”的表,我想确定是否通过“ user_id”选择两行,它们是否具有相同的“ chat_id”。 the code works fine when i test it in phpmyadmin's sql section and return the result. 当我在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)

but when i write it in laravel's style i get this error: 但是当我以laravel的风格编写它时,出现此错误:

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

any help would be really great! 任何帮助都将非常棒!

DB::select returns an array, not an object. DB::select返回一个数组,而不是对象。

Use count($nums) instead. 请改用count($nums)

when you use DB::select() , the returned thing is the result. 当您使用DB::select() ,返回的是结果。 not the query builder object. 不是查询生成器对象。

you can tackle this in three ways. 您可以通过三种方式解决此问题。

  • run another SELECT COUNT(id).... 运行另一个SELECT COUNT(id)....
  • if you are selecting ALL the rows, then count the items in array. 如果选择所有行,则计数数组中的项目。
  • change it to query builder. 将其更改为查询生成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM