繁体   English   中英

Laravel 5:SQLSTATE [23000]:违反完整性约束

[英]Laravel 5: SQLSTATE[23000]: Integrity constraint violation

我有一个包含以下字段的表单:名称,全名,描述,电子邮件,密码,州,城市(选择状态时通过Ajax填写)和照片(上传图片)。 这是我的架构:

public function up()
{
    Schema::create('states', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('acronym');
        $table->string('name');
    });

    Schema::create('cities', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->integer('state_id')->unsigned();
        $table->foreign('state_id')->references('id')->on('states');
    });

    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->string('fullname');
        $table->string('photo');
        $table->text('description');
        $table->string('email');
        $table->string('password', 60);
        $table->integer('city_id')->unsigned();
        $table->foreign('city_id')->references('id')->on('cities');
        $table->rememberToken();
    });
}

我的问题是,当我按下“提交”按钮时,出现以下错误消息:

SQLSTATE [23000]:完整性约束违规:1452不能添加或更新子行,外键约束失败( yearbookusers ,约束users_city_id_foreign外键( city_id )参考文献citiesid (SQL)):插入到usersnamedescriptionemail )值(Gabriel,Description, email gmail.com)

因此,问题与city_id是外键有关(表单中选项的'value'属性是id)。 来回示例:

<option value="281">Abaíra</option>

但是,当我尝试通过phpmyadmin插入时,它没有任何错误地插入。

那么可能是什么问题呢?

这是我在控制器上的保存方法:

public function saveRegister()
{
    $rules = array(
        'name' => 'required',
        'description' => 'required',
        'fullname' => 'required',
        'email' => 'required',
        'password' => 'required',
        'state' => 'required',
        'city' => 'required',
        'photo' => 'required',
        'photo' => 'image|max:3000000',
    );
    $val = \Validator::make(\Input::all(), $rules);

    if($validar->fails()){
        return redirect('register')->withErrors($val);
    } else {
        $user = new User(array(
            'name' => \Input::get('name'),
            'description' => \Input::get('description'),
            'fullname' => \Input::get('fullname'),
            'email' => \Input::get('email'),
            'city_id' => \Input::get('city'),
        ));
        $user->timestamps = false;
        $user->save();
        return redirect('register')->with('message', 'User saved!');

    }
}

编辑:固定在saveRegister()方法中的city_id。

代替

$user = new User(array(
        'name' => \Input::get('name'),
        'description' => \Input::get('description'),
        'fullname' => \Input::get('fullname'),
        'email' => \Input::get('email'),
        'city' => \Input::get('city'),
    ));

做这个

$user = new User();
$user->fill(array(
        'name' => \Input::get('name'),
        'description' => \Input::get('description'),
        'email' => \Input::get('email')
    ));
$user->fullname = \Input::get('fullname');
$user->city= \Input::get('city');

或更改模型中的$fillable属性

可能是因为您的city.id字段未签名,而FK字段未签名。 关系中使用的所有列必须完全匹配。

尝试将代码更改为此:

Schema::create('cities', function(Blueprint $table)
{
    $table->increments('id')->unsigned();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM