【发布时间】:2021-08-31 06:41:26
【问题描述】:
以下代码显示错误(在if ($response) { 行):
未定义变量:响应
我正在检查foreach 中的if 条件,因为我想检查UserEnabledNotifications 表中的每个id 是否存在于notifications 表中。在foreach 的if 条件中的dump($response); 也显示数据。
我可以在foreach 循环之外获取$response 中的数据吗?我该尝试什么?
$notificationData = UserEnabledNotifications::all();
foreach ($notificationData->where('status', 'true') as $user => $value) {
if (Notifications::where('userEnabledNotificationsId', $value['id'])->exists() == false) {
$notificationTypeName = NotificationTypes::where('id', $value['notificationTypesId'])
->value('notificationTypeName');
$userData = User::where('id', $value['userId'])
->get()
->toArray();
$data = [];
$data['notificationTypesId'] = $value['notificationTypesId'];
$data['notificationTypeName'] = $notificationTypeName;
$data['userId'] = $value['userId'];
$data['email'] = $userData[0]['email'];
$data['recipientName'] = $userData[0]['FullName'];
$data['userEnabledNotificationsId'] = $value['id'];
$response = Notifications::create($data);
//dump($response);
$tags[] = $response;
}
}
if ($response) {
return response()->json([
'message' => 'success',
'data' => $tags,
'statusCode' => 200,
'status' => 'success'
], 200);
}
【问题讨论】:
标签: php laravel eloquent laravel-8