【发布时间】:2021-11-10 13:03:27
【问题描述】:
我对我网站上的一些更新感到满意,但我不知道我做错了什么。
DB::beginTransaction();
try {
Log::debug('--- create ---');
Log::debug($responsible->contactInfo);
$firstName = isset($responsible->firstName) ? $responsible->firstName : 'NoName';
$lastName = isset($responsible->lastName) ? $responsible->lastName : $firstName;
$middleName = isset($responsible->middleName) ? $responsible->middleName : '';
if (isset($responsible->contactInfo)) {
foreach ($responsible->contactInfo as $key => $value) {
if ($value && property_exists($value, 'type') && $value->type == 'email') {
$email = $value->value;
}
if ($value && property_exists($value, 'type') && $value->type == 'phone') {
$phone = preg_replace('/[^0-9]/', '', $value->value);
}
}
}
我正在处理此信息:
(
[id] => 1000005
[firstName] => Test
[lastName] => John
[middleName] => Smith
[contactInfo] => stdClass Object
(
[0] => stdClass Object
(
[type] => email
[value] => testtest@gmail.com
)
[1] => stdClass Object
(
[type] => phone
[value] => +380 67 777 7777
)
)
[position] => Manager
)
我得到了这个错误:
[2021-09-14 15:46:18] production.INFO: responsible
[2021-09-14 15:46:18] production.INFO: stdClass Object
(
[id] => 1000005
[firstName] => Test
[lastName] => John
[middleName] => Smith
[contactInfo] => stdClass Object
(
[0] => stdClass Object
(
[type] => email
[value] => testtest@gmail.com
)
[1] => stdClass Object
(
[type] => phone
[value] => +380 67 777 7777
)
)
[position] => Manager
)
[2021-09-14 15:46:18] production.DEBUG: --- create ---
[2021-09-14 15:46:18] production.ERROR: Uncaught exception 'ErrorException' with message 'Object of class stdClass could not be converted to string' in /home/www/test/vendor/monolog/monolog/src/Monolog/Logger.php:329
我希望有人可以帮助我。
【问题讨论】:
-
Log::debug 接受字符串,您是否将其传递给对象?错误信息是这样说的。如果你真的需要像这样调试它,你可以将它转换为 json 字符串,例如,
Log::debug(json_encode((array)$responsible->contactInfo))。 -
将对象转成json
Log::debug($responsible->contactInfo->toJson());