The below code shows the error (on the line if ($response) {):
Undefined variable: response
I am checking the if condition inside the foreach because I wanted to check whether each id in the UserEnabledNotifications table exists in notifications table. Also dump($response); inside the if condition of foreach shows data.
Can I get the data in $response outside the foreach loop? What shall I try?
$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);
}
 
     
     
    