保存发送到数据库的群发短信记录 laravel

save the record of bulk sms sent into db laravel

我已经从用户 table.i 那里获取了 id 和 mobile_num 必须在此 table 中插入该 id 名称作为 user_id,mobile_num, and status(0,1) 到另一个 table(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{

       }
   }
}

这样做:

    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 {
    
                }
        }
    }
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[]