Reputation:
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);
}
Upvotes: 0
Views: 1085
Reputation: 426
Simply declare a null
or an empty array in a $response
variable and you will be able to get the data out of the loop!
Upvotes: 0
Reputation: 165
You might create a private or protected variable, and put it outside, and then access it directly or via functions
$notificationData = UserEnabledNotifications::all();
private $reponse = null;
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);
$tags[] = $response;
}
}
if ($response) {
return response()->json([
'message' => 'success',
'data' => $tags,
'statusCode' => 200,
'status' => 'success'
], 200);
}
But now each place you would need to check whether responses are null or not.
Check this answer : What is the difference between public, private, and protected?
I quote
- public scope to make that property/method available from anywhere, other classes, and instances of the object.
- private scope when you want your property/method to be visible in its own class only.
- protected scope when you want to make your property/method visible in all classes that extend current class including the parent class.
Upvotes: 0
Reputation: 1182
You define $response
in first if body but you need $response = null
above that.
Upvotes: 2