正如标题所说,我正在尝试将一个数组放入一个数组中,并希望获得带有地址信息的用户信息。当我对其进行调试时,它会以正确的结果进行迭代,但是在获得所有结果之后,将显示一条错误消息,提示“正在尝试获取非对象的属性'addressLine1'”。
protected function filterUsers($users, $keys = [])
{
$data = array();
foreach ($users as $user) {
$entry = [
'username' => $user->username,
'email' => $user->email,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'phone' => $user->phone,
'emergency_phone' => $user->emergency_phone,
'gender' => $user->gender,
'profile_photo' => $user->profile_photo,
'role' => $user->role,
'status' => $user->status,
'href' => route('users.show', ['id' => $user->user_id])
];
if (in_array('address', $keys)) {
$entry['address'] = [
'addressLine1' => $user->address->addressLine1,
'addressLine2' => $user->address->addressLine2,
'city' => $user->address->city,
'post_code' => $user->address->post_code,
];
}
$data[] = $entry;
}
return $data;
}