-2

我已经从用户表中获取了 id 和 mobile_num。我必须将该表中的 id 名称作为 user_id、mobile_num 和 status(0,1) 插入另一个表 (wc_sms_status)。sendSMSFunction 工作正常。

  public function SendBulkSms()
  {  
    $usersNumber = User::select('id','mobile_num')->whereIn('id', [5,6,7,8])->get();  
    foreach($usersNumber as $userNumber)
    {  
    if (!$userNumber->mobile_num) 
    {
        $this->sendSmsFunction($userNumber->mobile_num);  
        DB::table('wc_sms_status')->insert([
            ['user_id' => 'id'],
            ['mobile_num' => 'mobile_num'] // set the status=1 // how query can be changed?
        ]);
       
    } 
    elseif($userNumber->mobile_num == exist && status == 0)
    {
        $this->sendSmsFunction($userNumber->mobile_num);
        $this->save();
    }
    else{

       }
   }
}
4

2 回答 2

0

做这个 :

    public function SendBulkSms()
      {  
//assuming there is a relationship between your model users and wc_sms_status called wcSmsStatus

        $usersNumber = User::with('wcSmsStatus')->select('id','mobile_num')->whereIn('id', [5,6,7,8])->get();  
        foreach($usersNumber as $userNumber)
        {  
          if (!$userNumber->mobile_num) 
            {
                $this->sendSmsFunction($userNumber->mobile_num);  
                DB::table('wc_sms_status')->insert([
                    'user_id' => $userNumber->id,
                    'mobile_num' => $userNumber->mobile_num, 
                    'status' => 1, 
                ]);
            } elseif ($userNumber->mobile_num && $userNumber['wcSmsStatus']->status === 0)
                {
                   $this->sendSmsFunction($userNumber->mobile_num);
                   $this->save();
                } else {
    
                }
        }
    }
于 2021-09-13T13:25:39.550 回答
0
public function SendBulkSms()
{
    $users = User::select('id','mobile_num')
        ->whereIn('id', [5,6,7,8])
        ->whereNotNull('mobile_num')
        ->get();

    $bulkData = [];
    foreach ($users as $user)
    {
        $this->sendSmsFunction($userNumber->mobile_num);
        DB::table('wc_sms_status')->insert([
            ['user_id' => 'id'],
            ['mobile_num' => 'mobile_num'] // set the status=1 // how query can be changed?
        ]);
        $bulkData[] = [
            'user_id'        => $user->id,
            'mobile_num'     => $user->mobile_num,
        ];
    }

    if (!empty($bulkData)) {
        WcSmsStatus::insert($education);  // change to your model name
        unset($bulkData);
    }
}

尝试这样使用,它会插入大量数据,不要在模型中提到受保护的 $fillable[]

于 2021-09-13T13:33:28.697 回答