0

I have function where I try to search in 2 columns for matched id but in all times it return true even where there is actually no result.

I want to search for users previous relations but not sure how to make this logic happen.

Logic

  1. My table has 2 columns from_id and to_id (both will get users ids)
  2. I am sending 2 ids to back-end (one as input, another one in request header)
  3. I want to say: where input id is from_id and header id is to_id , ORwhere input id is to_id and header id is from_id return the result otherwise is null obviously.

Code

public function check(Request $request) { // always return true!
        $user = $request->user();
        $userInput = $request->input('receiverId');
        $chat = PrivateChat::where(function ($query) use ($user) {
            $query->where('from_id', $user->id)->orWhere('to_id', $user->id);
        })->orWhere(function ($query) use ($userInput) {
            $query->where('from_id', $userInput)->orWhere('to_id', $userInput);
        })->first();

        $receiver = User::where('id', $request->input('receiverId'))->first();

        if($chat) {
            return response()->json([
                'data' => new PrivateChatResource($chat),
                'exist' => true,
                'receiver' => new UserResource($receiver),
                'message' => 'Chat data retrieved successfully.'
            ]);
        } else {
            return response()->json([
                'exist' => false,
                'receiver' => new UserResource($receiver),
                'message' => 'Chat data retrieved successfully.'
            ]);
        }
}

Note: this is follow up of my older question.

Any idea what is the issue?

Update

Based on Giacomo M comment i think this should be the final code

$chat = PrivateChat::where(function ($query) use ($user, $userInput) {
            $query
                ->where('from_id', $user->id)
                ->where('to_id', $userInput);
        })->orWhere(function ($query) use ($user, $userInput) {
            $query
                ->where('from_id', $userInput)
                ->where('to_id', $user->id);
        })->first();

makes sense?

1 Answer 1

0

You use orWhere also in the subqueries, but according to your logic you need AND in the subqueries.

Your query becomes:

$chat = PrivateChat::where(function ($query) use ($user) {
    $query
        ->where('from_id', $user->id)
        ->where('to_id', $user->id); // where, NOT orWhere
})->orWhere(function ($query) use ($userInput) {
    $query
        ->where('from_id', $userInput)
        ->where('to_id', $userInput); // where, NOT orWhere
})->first();
Sign up to request clarification or add additional context in comments.

3 Comments

very good, i think they also need to use $userInput and $user->id in each set, not only for 1 of the sets each
I think the problem is you use same id for both columns >where('from_id', $userInput) | ->where('to_id', $userInput) and ->where('from_id', $user->id) | ->where('to_id', $user->id)
shouldn't be like ->where('from_id', $user->id) | ->where('to_id', $userInput)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.