Laravel 5.2一对一的关系给出了一个错误消息" SQLSTATE [23000]:"表格提交后

时间:2016-03-10 11:15:16

标签: php mysql laravel laravel-5.2

我在laravel 5.2中创建了两个表,一个叫做#34;用户"而另一个被称为" artists_details"他们有一对一的关系。 users表的架构如下

Schema::create('users', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->boolean('admin')->nullable();
        $table->boolean('manager')->nullable();
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->rememberToken();
        $table->timestamps();
    });

和艺术家表的架构如下

Schema::create('artists_details', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->integer('user_id')->unsigned();
        $table->string('artists_image_path');
        $table->string('name');
        $table->integer('phone_no');
        $table->integer('passport');
        $table->string('city');
        $table->string('county');
        $table->string('facebook_name');
        $table->string('twitter_handle');
        $table->string('email');
        $table->string('alternative_email');
        $table->string('website');
        $table->text('biography');
        $table->timestamps();

        $table->foreign('user_id')
              ->references('id')
              ->on('users')
              ->onDelete('CASCADE');

    });

我已经在模型中表明了如下关系 用户模型

public function artists_relation()
{
    return $this->hasOne('App\artists_details_model');
}

和artists_details_model如下

public function user_artist_details()
{
    return $this->belongsTo('App\User');
}

处理控制器中表单提交的php代码如下

public function artists_details_store(Request $request)
{
  $input = \Request::all();
  $file = $request->file('file');

   $name = time(). $file->getClientOriginalName();

   $file->move('artists_image/photo', $name);



  $artists_input = new artists_details_model;

  $artists_input->artists_image_path  = 'artists_image/photo/'. $name;
  $artists_input->name                = $input['name'];
  $artists_input->phone_no            = $input['phone_no'];
  $artists_input->passport            = $input['passport'];
  $artists_input->city                = $input['city'];
  $artists_input->county              = $input['county'];
  $artists_input->facebook_name       = $input['facebook_name'];
  $artists_input->twitter_handle      = $input['twitter_handle'];
  $artists_input->email               = $input['email'];
  $artists_input->alternative_email   = $input['alternative_email'];
  $artists_input->website             = $input['website'];
  $artists_input->biography           = $input['biography'];
  $artists_input->save();


    return redirect('create');
}

当我点击提交按钮时,我收到以下错误消息

  

Connection.php第669行中的QueryException:

     

SQLSTATE [23000]:完整性约束违规:1452无法添加或   更新子行:外键约束失败

我似乎无法看到我出错的地方或似乎是什么问题

2 个答案:

答案 0 :(得分:1)

对于表格' artists_details' ,您提到了

$table->foreign('user_id') ->references('id') ->on('users')

因此,每当您尝试将详细信息保存在' artists_details'你需要提供' user_id'没有它,信息将无法保存。

您需要传递' UserID'作为隐藏参数或者如果UserID保存在Session中,则需要从Session中检索它。

答案 1 :(得分:0)

您似乎没有在表单中包含外键{user_id}