SQLSTATE [23000]:完整性约束违规:1048列“接收者”不能为空

时间:2018-01-07 03:06:10

标签: php laravel

我查看了之前关于这个主题的所有帖子,我似乎无法找到我做错了什么。

我有一个双向短信平台,它允许我从Twilio接收消息,然后将它们保存在我的数据库中。

保存电话号码和邮件正文没有问题,但是当我尝试保存媒体和邮件中的媒体数量时,我收到错误。

完整错误: Error Image

我的回复Twilio代码

    public function replyTwilio(Request $request)
{
    $number = $request->input('From');
    $to = $request->input('To');
    $body = $request->input('Body');
    $numMedia = $request->input('NumMedia');
    $mediaUrl = '';

    if ($number == '' && $body = '' && $to == '') {
        return 'Invalid Request';
    }

if ($numMedia != 0) {
    for ($i = 0; $i <= $numMedia; $i++) {
        $mediaUrl = $request->input('MediaUrl' + $i) + ',';
    }
}
    $clphone = str_replace(" ", "", $number); #Remove any whitespace
    $clphone = str_replace('+', '', $clphone);

    $msgcount = strlen($body);
    $msgcount = $msgcount / 160;
    $msgcount = ceil($msgcount);

    $get_status = $this->insertSMS($clphone, $msgcount, $body, $to, 1, $mediaUrl, $numMedia);

    if ($get_status) {
        return 'success';
    } else {
        return 'failed';
    }

}

InsertSMS():

    public function insertSMS($number, $msg_count, $body, $to = '', $gateway = '', $media, $num_media)
{
    $get_info = SMSHistory::where('receiver', 'like', '%' . $number)->orderBy('id', 'desc')->first();

          if ($get_info) {

        if (strtolower($body) == 'stop'){
            BlackListContact::create([
                'user_id' => $get_info->userid,
                'numbers' => $number
            ]);

            $gateway_info = SMSGateways::find($gateway);
            $this->dispatch(new SendBulkSMS($get_info->userid, $number, $gateway_info, $to, 'You will no longer receive any messages', '1', null));

        }


        if (strtolower($body) == 'start'){
            $contact = BlackListContact::where('numbers',$number)->first();
            if ($contact){
                $contact->delete();
            }
        }

        $status = SMSHistory::create([
            'userid' => $get_info->userid,
            'sender' => $number,
            'receiver' => $to,
            'message' => $body,
            'amount' => $msg_count,
            'status' => 'Success',
            'api_key' => null,
            'use_gateway' => $gateway,
            'send_by' => 'receiver',
            'sms_type' => 'text',
            'media_url' => $media,
            'num_media' => $num_media
        ]);
    } else {

        if (strtolower($body) == 'stop'){
            BlackListContact::create([
                'user_id' => 0,
                'numbers' => $number
            ]);

            $gateway_info = SMSGateways::find($gateway);
            $this->dispatch(new SendBulkSMS(0, $number, $gateway_info, $to, 'You will no longer receive any messages', '1', null));
        }

        $status = SMSHistory::create([
            'userid' => 0,
            'sender' => $number,
            'receiver' => $to,
            'message' => $body,
            'amount' => $msg_count,
            'status' => 'Success',
            'api_key' => null,
            'use_gateway' => $gateway,
            'send_by' => 'receiver',
            'sms_type' => 'text',
            'media_url' => $media,
            'num_media' => $num_media
        ]);
    }

    if ($status) {
        return true;
    } else {
        return false;
    }


}

SMSHistory的模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class SMSHistory extends Model
{
protected $table='sys_sms_history';
protected $fillable=['userid','sender','receiver','message','amount','status','api_key','use_gateway','send_by','sms_type','media_url','num_media'];
}

1 个答案:

答案 0 :(得分:0)

如果您需要$number$body$to变量,请尝试以下操作:

if ($number == '' || $body = '' || $to == '') {
    return 'Invalid Request';
}

&&运算符替换||运算符。如果其中任何一个为空,则返回Invalid Request