php - Laravel Query Builder orWhere returns false result -


i need query counts conversations not seen yet. works fine when want check if there conversations from_id returns wrong result.

conversation::where('to_id', $this->id)->whereexists(function($query) {     $query->select(db::raw(1))         ->from('messages')         ->where('seen', 0)         ->whereraw('messages.conversation_id = conversations.id'); })->count(); 

result: 0

conversation::where('from_id', $this->id)->whereexists(function($query) {     $query->select(db::raw(1))         ->from('messages')         ->where('seen', 0)         ->whereraw('messages.conversation_id = conversations.id'); })->count(); 

result: 0

conversation::where('to_id', $this->id)->orwhere('from_id', $this->id)->whereexists(function($query) {     $query->select(db::raw(1))         ->from('messages')         ->where('seen', 0)         ->whereraw('messages.conversation_id = conversations.id'); })->count(); 

result 1

so run them both , add result there has better solution.

update: forgot logic part of orwherestatement. query true when left part true or right part. when to_id equal id whole query true.

i solved putting or statement in function:

conversation::where(function($query) {     $query->where('to_id', $this->id)         ->orwhere('from_id', $this->id); })->whereexists(function($query) {     $query->select(db::raw(1))         ->from('messages')         ->where('seen', 0)         ->whereraw('messages.conversation_id = conversations.id'); })->count(); 


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -