簡體   English   中英

laravel原始查詢不返回結果

[英]laravel raw query doesn't return results

我有下面的代碼

$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

任何幫助都將非常棒!

DB::select返回一個數組,而不是對象。

請改用count($nums)

當您使用DB::select() ,返回的是結果。 不是查詢生成器對象。

您可以通過三種方式解決此問題。

  • 運行另一個SELECT COUNT(id)....
  • 如果選擇所有行,則計數數組中的項目。
  • 將其更改為查詢生成器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM