【问题标题】:Laravel get data out of foreach loopLaravel 从 foreach 循环中获取数据
【发布时间】:2021-08-31 06:41:26
【问题描述】:

以下代码显示错误(在if ($response) { 行):

未定义变量:响应

我正在检查foreach 中的if 条件,因为我想检查UserEnabledNotifications 表中的每个id 是否存在于notifications 表中。在foreachif 条件中的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


    【解决方案1】:

    您首先在 if 正文中定义 $response,但您需要在上面定义 $response = null

    【讨论】:

      【解决方案2】:

      您可以创建一个私有或受保护的变量,并将其放在外面,然后直接或通过函数访问它

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

      但是现在每个地方都需要检查响应是否为空。

      为什么是私有的、受保护的或公开的?

      检查这个答案:What is the difference between public, private, and protected?

      我引用

      • 公共范围,使该属性/方法可在任何地方、其他类和对象实例中使用。
      • 当您希望您的属性/方法仅在其自己的类中可见时的私有范围。
      • 当您想让您的属性/方法在所有扩展当前类(包括父类)的类中可见时的受保护范围。

      【讨论】:

      • 这仍然会返回相同的错误,并没有真正改变
      • 抱歉,我没明白你的意思@brombeer
      • private $response; 行显示syntax error, unexpected private
      • @Mege 你能在这一行上面显示代码吗,也许你没有关闭一些东西
      • 正在关闭一切。整个代码在类的函数中给出。那是问题吗?像这样 - class UserNotificationsController extends Controller { public function insert(Request $request) {
      【解决方案3】:

      只需在$response 变量中声明一个null 或一个空数组,您就可以将数据从循环中取出!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-08
        • 2020-01-22
        • 1970-01-01
        • 2016-04-29
        • 1970-01-01
        • 1970-01-01
        • 2019-11-03
        • 2020-12-11
        相关资源
        最近更新 更多