Home > Back-end >  Laravel 7: Showing error while passing multiple variable in str_replace
Laravel 7: Showing error while passing multiple variable in str_replace

Time:08-03

I'm facing error while passing multiple variable in str_replace function. Error: Argument 1 passed to Xenon\LaravelBDSms\SMS::shoot() must be of the type string, null given, called in

Message Body:

Hello #name#,
Total Amount Purchased : #total#
Previous Due: #previous_due#
Deposit: #deposit#
Total Due: #total_due#

Controller:

$id = 1;
$sms_settings = SmsSetting::findOrFail($id);
if($sms_settings->order_create == 1){
    $name = $request->name;
    $previous_due = $customer->due;
    $deposit = $request->deposit;
    $total = $request->total;
    $total_due = $request->total_due;
    $msgs =  $sms_settings->order_create_sms;
    $msg = str_replace(array('#name#', '#total#','#previous_due#','#deposit#','#total_due#'), array($name,$previous_due, $deposit, $total, $total_due), $msgs);
    $send= SMS::shoot($request->mobile, $msg);
}

Shoot Function:

public function shoot(string $number, string $text)
  {
     $this->sender->setMobile($number);
     $this->sender->setMessage($text);
     return $this->sender->send();
  }

Here I'm using a Laravel Package for sending SMS to mobile number. How can I pass multiple variable in str_replace?

CodePudding user response:

I think the variable $msgs = $sms_settings->order_create_sms; contain empty that's why str_replace couldn't replace the data that you given so $msg = str_replace(array('#name#', '#total#','#previous_due#','#deposit#','#total_due#'), array($name,$previous_due, $deposit, $total, $total_due), $msgs); , will return null. I recommend checking $msgs again.

CodePudding user response:

  $msgs =  $sms_settings->order_create_sms;

Make sure $msgs is not null place is_null($msgs) condition before feeding to str_replace

check more about str_replace: https://www.php.net/manual/en/function.str-replace.php

  • Related