dongsi2317 2015-05-06 18:03
浏览 103
已采纳

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

I have a form with the following fields: name, fullname, description, email, password, state, city (filled through Ajax when the state is chosen) and photo (a image upload). Here's my schema:

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

My problem is that when I press the submit button, the following error message appears:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (yearbook.users, CONSTRAINT users_city_id_foreign FOREIGN KEY (city_id) REFERENCES cities (id)) (SQL: insert into users (name, description, email) values (Gabriel, Description, email@gmail.com))

So the problem is related to the city_id being a foreign key (the 'value' attribute from the option at the form is the id). Fro exemple:

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

But when I try to insert through phpmyadmin, it inserts with no errors.

So what could be the problem?

Here's my save method at the controller:

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!');

    }
}

Edit: fixed city_id at saveRegister() method.

  • 写回答

2条回答 默认 最新

  • dropbox1111 2015-05-06 18:37
    关注

    instead of

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

    do this

    $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');
    

    or change the $fillable property in the model

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?