Laravel 5.2 一对一关系在提交表单后给出错误消息 "SQLSTATE[23000]:"

Laravel 5.2 one to one relationship gives an error mesg "SQLSTATE[23000]:" after form submission

我在 laravel 5.2 中创建了两个 table,一个叫做 "users",另一个叫做 "artists_details",它们是一对一的关系。用户 table 的架构如下

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

和艺术家table的架构如下

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

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

QueryException in Connection.php line 669:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

我似乎看不出哪里出了问题或者问题出在哪里

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

对于您提到的 table 'artists_details'

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

因此,无论何时您尝试在 'artists_details' 中保存详细信息,您都需要提供 'user_id',否则信息将无法保存。

要么你需要将'UserID'作为隐藏参数传递,要么如果UserID保存在Session中,那么你需要从Session中检索它。