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
array_keys($json)andwhereIn()Side note: consider using good variable names while programming: you've got a variable$jsonthat holds an array. That is confusing :-)