Laravel Query Inside Json Loop

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", "<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f6c70727a40727e76735f78727e7673317c7072">[email protected]</a>":"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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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

Method 2

$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 ";
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x