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 orwhere
statement. 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
Post a Comment