0

Querying inside a json loop always returns exists even when row does not exist.

Hi I have a Json Object which looks like this

{"+888588888":"Person 1", "[email protected]":"Person 2"}

I am using the following code to check whether the record exists in the table:

    // Get The Json From Source
    $json = json_decode($request->getContent(), true);

    // Loop Through Json And Insert Into Mysql
    foreach ($json as $key => $value) {

        $result = UserInvitesModel::where('mysql_user_id', $mysql_user_id)
            ->where(function ($q) use ($key, $value) {
                $q->where('phone', $key)
                    ->orWhere('email', $key);
            })->get();

        if (empty($result)) {
            echo "does-not-exist ";
        } else {
            echo "exists ";
        }
    }

I am always getting exists

6
  • What do you expect as a result? Only one? Multiple? Commented Sep 14, 2021 at 7:11
  • result for each key Commented Sep 14, 2021 at 7:13
  • Is this to check one user? then why are you using a loop instead of WHERE IN Commented Sep 14, 2021 at 7:13
  • yes its for one user Commented Sep 14, 2021 at 7:14
  • 1
    In that case you can do it in one query instead of using a foreach loop. You only user the keys from the array so use array_keys($json) and whereIn() Side note: consider using good variable names while programming: you've got a variable $json that holds an array. That is confusing :-) Commented Sep 14, 2021 at 7:17

2 Answers 2

1

You are using empty() on a Collection and that's the problem.

The ->get() method you use returns a Collection and in order to check if it has at least one element, you have to use isEmpty():

// Get The Json From Source
    $json = json_decode($request->getContent(), true);

    // Loop Through Json And Insert Into Mysql
    foreach ($json as $key => $value) {

        $result = UserInvitesModel::where('mysql_user_id', $mysql_user_id)
            ->where(function ($q) use ($key, $value) {
                $q->where('phone', $key)
                    ->orWhere('email', $key);
            })->get();

        if ($result->isEmpty()) {
            echo "does-not-exist ";
        } else {
            echo "exists ";
        }
    }

empty() always returns false on Collection:

empty(collect()); // false
collect()->isEmpty(); //true
Sign up to request clarification or add additional context in comments.

Comments

1

$result is never "empty"!! even if no record is returned, it still is an instance of a Collection::class with empty array as items.

You should test on $result->count() (a method of the collection class)

or improve your code.

foreach ($json as $key => $value) {
    $count = UserInvitesModel::where('mysql_user_id', $mysql_user_id)
        ->where(function ($q) use ($key, $value) {
            $q->where('phone', $key)
                ->orWhere('email', $key);
        })->count(); //return an integer

    if (!$count) {
        echo "does-not-exist ";
    } else {
        echo "exists ";
    }
}

if you need the user entity use first instead

foreach ($json as $key => $value) {
    $user = UserInvitesModel::where('mysql_user_id', $mysql_user_id)
        ->where(function ($q) use ($key, $value) {
            $q->where('phone', $key)
                ->orWhere('email', $key);
        })->first(); //returns null or an instance of the model

    if (!$user) {
        echo "does-not-exist ";
    } else {
        echo "exists ";
    }
}

1 Comment

var_dump($user); giving NULL... so used is_null($user) it seems to be working thanks for pushing in that directions

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.