當前在Laravel中運行查詢,但預期的輸出不正確。所以,看來我仍然不知道使用where/orwhere
的正確方法。我在下面有兩個查詢;Laravel:在查詢中使用Where和orWhere的正確方法
QUERY圖1(a)
//total friends
$user = Friend::select('friend_sender', 'friend_receiver')
->where('friend_sender', Input::get("user_id"))
->orwhere('friend_receiver', Input::get("user_id"))
->where('friendship_status','friends')->count();
QUERY圖1(b)
//total friends
$user = Friend::select('friend_sender', 'friend_receiver')
->where('friendship_status','friends')
->where('friend_sender', Input::get("user_id"))
->orwhere('friend_receiver', Input::get("user_id"))->count();
這兩個查詢產生不同的結果。 QUERY 1 (a)
結果是0
,這在我自己的例子中是正確的,QUERY 1 (b)
結果是1
這是錯誤的。
現在,我認爲其他WHEREs
應該在WHERE/orWHERE
陳述之後。我寫了另一個查詢,但沒有得到所需的結果。
$user = Friend::select('friend_sender', 'friend_receiver')
->where('friend_sender',Input::get("user_id"))
->orwhere('friend_receiver',Input::get("user_id"))
->where('sender_follow', 'yes')
->orwhere('receiver_follow', 'yes')
->where('friendship_status', 'friends')->count();
我的問題是,我該如何正確使用WHERE/orWHERE
?