table($table); foreach ($data as $key => $value) { $query->where($key, $value); } $record = $query->first(); if (!$record) { // Check if a record exists but doesn't match the expected attributes $actualRecord = DB::connection($connection)->table($table)->where('norujukan', $data['norujukan'])->first(); if ($actualRecord) { $mismatchedColumns = []; foreach ($data as $key => $value) { if ($actualRecord->$key !== $value) { $expectedValue = stripslashes($value); // Remove backslashes from expected $mismatchedColumns[$key] = [ 'expected' => $expectedValue, 'actual' => $actualRecord->$key, ]; } } PHPUnit::fail( "A record exists in the table [{$table}] but does not match the expected attributes. " . "Mismatched columns: " . json_encode($mismatchedColumns) ); } PHPUnit::fail( "Failed asserting that a record exists in the table [{$table}] with the attributes " . json_encode($data) ); } PHPUnit::assertNotNull($record, "Record exists and matches the expected attributes."); } }