1

I am facing a really weird problem. The problem is shortly:

$x = some_object;
return response()->json($x)            =>  {}  
return response()->json(some_object)   =>  {some_object}

This is all of the code(The code goes inside the else statement):

public function checkCalls(Request $req)
{
    $active_call = VideoCall::where("receiver_id", (int) $req->input("user_id"));

    if ($active_call->where("call_situation", "call")->exists()) {
        $active_call = $active_call->first();

        $caller_id = $active_call->caller_id;
        $caller = User::where("id", $caller_id)->first();
        $caller_name = $caller->name;
        $caller_img = $caller->image;
        return response()->json([
            "friend" => $caller_id,
            "caller_name" => $caller_name,
            "caller_img" => $caller_img
        ]);
    } else if ($active_call->where("call_situation", "yes")->exists()) {
        return response()->json("gorusme yapıyor");
    } else if ($active_call->where("call_situation", "decline")->exists()) {
        $caller_id = $active_call->where("call_situation", "decline")->first()->caller_id;
        return response()->json("arama red edildi", $caller_id);
    } else {
        return response()->json("arama yok"); 
        //this code runs, I m changing here to get different returns  
    }
}

When I change inside the else brackets, this returns true

return response()->json(VideoCall::where("receiver_id", (int) $req->input("user_id"))->exists());

But this returns false:

return response()->json($active_call->exists());

$active_call returns an empty object for some reason. The only explanation is that $active_call changes in if() parentheses but I can't find anything that can change it. So why is this happening?

*Edit: Made the question clearer, added a shorter version of the problem.

*Edit 2: The code inside the else statement is running. I m changing that return statement to get different results

1
  • Please improve your question title; it is incredibly vague and is very unlikely to attract researchers that are suffering the same issue. Commented May 15, 2020 at 5:08

1 Answer 1

1

It's because you're re-assigning $active_call in your function:

$active_call = $active_call->first();

It's no longer a Builder, but is instead a VideoCall.

So, this line:

return response()->json($active_call->exists());

Is actually:

return response()->json(VideoCall::where("receiver_id", (int) $req->input("user_id"))->first()->exists());

Note the additional ->first()

Sign up to request clarification or add additional context in comments.

9 Comments

No the code inside the else statement runs. It's not going in the first if statement. I should specify that in the question though. Its kinda confusing
I fixed it, sorry for the confusion.
Also, I tried to run the code by commenting the re-assignment line. It's still the same.
Running ->where() on a Builder modifies the builder. Try running return response()->json($active_call->exists()); immediately after you assign $active_call and it will work as expected. The ->where() calls in your if and else ifs are changing $active_call too.
Also, use dd($active_call->toSql()) to debug your query. The query you're building isn't what you think it is.
|

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.