2017-10-08 105 views
0

當我點擊提交(保存)按鈕以創建一個Job ...然後我得到下面的異常....(user_name field已被假定爲隱藏在作業中.create.blade查看錶單)SQLSTATE [23000]:Laravel中的完整性約束違規

你能告訴我在哪裏修改修復? 謝謝。

enter image description here

(3/3)QueryException

SQLSTATE [23000]:完整性約束違規:1452不能添加或更新 子行:外鍵約束失敗(admin-dbjobs, CONSTRAINT jobs_customer_name_foreign FOREIGN KEY(customer_name) REFERENCES customerscustomer_name))(SQL:insert into jobsuser_namecustomer_namejob_placejob_typenote_1time_usedupdated_atcreated_at)值(約翰,1,KONTOR, 多梅內OG Webhotell,ASDF,2017年10月8日零時二十三分40秒,2017-10 -08 0點23分40秒))

timestamp_create_users_table.php

public function up() 
{ 
    Schema::create('users', function (Blueprint $table) { 
     $table->engine = 'InnoDB';    
     $table->increments('id')->unsigned(); 

     $table->string('name')->index(); 
     $table->string('email'); 
     $table->string('password'); 
     $table->rememberToken(); 
     $table->timestamps(); 
    }); 
} 

timestamp_create_customers_table.php

public function up() 
{   
    Schema::create('customers', function (Blueprint $table) { 
     $table->engine = 'InnoDB'; 
     $table->integer('id')->unsigned(); 

     $table->string('customer_name')->index(); 
     $table->string('customer_email'); 
     $table->timestamps(); 
     $table->softDeletes(); 
    }); 
} 

timestamp_create_jobs_table.php

public function up() 
{ 

    Schema::create('jobs', function (Blueprint $table) { 
     $table->engine = 'InnoDB'; 

     $table->increments('id')->unsigned(); 

     $table->string('user_name')->index(); 
     $table->string('customer_name')->index();   
     $table->string('job_place'); 
     $table->string('job_type'); 
     $table->string('note_1'); 
     $table->time('time_used')->nullable(); 
     $table->timestamps(); 
     $table->softDeletes(); 

     $table->foreign('user_name')->nullable()->references('name')->on('users')->onDelete('cascade'); 
     $table->foreign('customer_name')->nullable()->references('customer_name')->on('customers')->onDelete('cascade'); 
    }); 
} 

模型關係定義:Job.php

public function users() 
{ 
    return $this->belongsTo(User::class); 
} 

public function customers() 
{ 
    return $this->belongsTo(Customer::class); 
} 

模型關係限定:Customer.ph p

public function jobs() 
{ 
    return $this->hasMany(Job::class); 
} 

回答

1

我自己得到了答案......

的問題是使用勇氣方法。下面的 經過修改後效果很好。

<div class="form-group col-sm-6"> 
     {!! Form::label('customer_name', 'Customer Name:') !!} 
     {!! Form::select('customer_name', $searchdata->pluck('customer_name', 'customer_name')->all(), null, ['class' => 'form-control']) !!} 
</div> 
相關問題