0

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);
}

3 Answers 3

2

You define $response in first if body but you need $response = null above that.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Why private or protected or public?

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.

7 Comments

That would still return the same error, nothing really changed
Sorry but I didn't got your point @brombeer
private $response; line shows syntax error, unexpected private
@Mege can you show the code just above this line, maybe you are not closing something
Am closing everything. this whole code is given inside a function in a class. Is that the problem? like this - class UserNotificationsController extends Controller { public function insert(Request $request) {
|
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.