1

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 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: whereinput id is from_id and header id is to_idORwhere input id is to_id and header id is from_id` return the result otherwise is null obviously.

Note: The part that I'm stuck in is and in conditions input id is from_id and header id is to_id OR where input id is to_id and header id is from_id

Code

currently I have half way code which is this:

public function check(Request $request) {
  // user id from header request (request sender) e.g `1`
  $user = $request->user();
  // second user id (not sure where to use it yet) e.g `4`
  $request->input('receiverId');

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

To make it simple

Basically I want return old chat between the 2 ids regardless of who was sender (from_id) and who was receiver (to_id).

Any idea how to make my logic happen?

Update

this is my full code and it always return false while i do have previous chat between users with id 1 and 4.

public function check(Request $request) {
    $user = $request->user();

    $chat = PrivateChat::where([['from_id', $user->id],['to_id',$request->input('receiverId')]])
    ->orWhere([['from_id', $request->input('receiverId')],['to_id',$user->id]])->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.'
        ]);
    }
}
1
  • NOTE: Due to the bug in answer of this question I've managed to fixed it here Commented Jun 22, 2020 at 7:37

2 Answers 2

1

As i see it you want to do the boolean logic, if wrong please correct me.

(from_id = header_id AND to_id = header_id) OR (from_id = input_id AND to_id = input_id)

This can be obtained with the following Eloquent query. Using where($cloure) syntax, for doing the boolean logic listed above. Calling where with a closure, adds parenthesis to your logic, everything within the clojure will be wrapped in ().

$user = $request->user();

$userInput = $request->input('receiverId');

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();
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much.
Hi @mrhn , I have problem with this code in all times it returns true, even when there is actually no record with provided data. Code gist.github.com/robertnicjoo/61554cd9f5853642e6eb62d79a49887d
I would assume the authenticated users has rows in the db?
0

If i understand correctly your where logic looks like this:

('from_id' = $user->id  &  'to_id' = $request->input('receiverId'))
|| ('from_id' = $request->input('receiverId')  &  'to_id' = $user->id)

You can define multiple conditions in a single where:

PrivateChat::where([['from_id', $user->id],['to_id',$request->input('receiverId')]])
->orWhere([['from_id', $request->input('receiverId')],['to_id',$user->id]])->first();

->first() will return the first record that meets one of the conditions, but if you want all results you can use ->get().

1 Comment

yes you understood it right. I shared an update please check it.

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.