save the record of bulk sms sent into db laravel

i have taken the id and mobile_num from users table.i have to insert that id name in this table as user_id,mobile_num,and status(0,1) into another table(wc_sms_status).sendSMSFunction is working fine.

  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{

       }
   }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Do this :

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

Method 2

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

try to use in this way, it will insert bulk data, dont fergot to mention protected $fillable[] in model


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x