2016-11-05 119 views
0

嘗試更新登錄後,用戶配置文件和我有此錯誤:SQLSTATE [23000]:完整性約束衝突:在Laravel 5

QueryException in Connection.php line 662: 
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`wallet`.`profiles`, CONSTRAINT `profiles_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into `profiles` (`gender`, `city`, `state`, `profession`, `aboutmyself`, `fb`, `twitter`, `gp`, `instagram`, `personal_site`, `aboutme`, `linkedin`, `pinterest`, `updated_at`, `created_at`) values (male, Upper Darby, Washington DC, Architects, Am a Benefactor of Grace and a Heir to the Throne a Royal Priesthood. I Love Jesus! s, url, url, url, url, url, url, hurl, url, 2016-11-05 09:35:51, 2016-11-05 09:35:51)) 

和本

PDOException in Connection.php line 390: 
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`wallet`.`profiles`, CONSTRAINT `profiles_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) 

意味着代碼的一部分正在執行,但「完整性違規」中的某些內容阻礙了數據的保存。

CONTROLLER(UserController.php)

public function update(Request $request) 
{ 

    $rules = [ 
     'name' => 'required', 
     'email' => 'required', 
     'phone' => 'required|numeric', 
     'country' => 'required', 
     'gender' => 'required', 
     'birthday' => 'required', 
     'fb' => 'url', 
     'twitter' => 'url', 
     'gp' => 'url', 
     'instagram' => 'url', 
     'personal_site' => 'url', 
     'aboutme' => 'url', 
     'linkedin' => 'url', 
     'pinterest' => 'url' 

    ]; 

    $data= $request->all(); 
    $validator = Validator::make($data, $rules); 
    if($validator->fails()){ 
     return Redirect::back()->withInput()->withErrors($validator); 
    } 

    $user = Auth::user(); 

    $user->name = $data['name']; 
    $user->email = $data['email']; 
    $user->phone = $data['phone']; 
    $user->country = $data['country']; 
    $user->birthday = $data['birthday']; 
    $user->address = $data['address']; 
    if($user->save()) { 
     $profile_id = $user->id; 
     $profile = Profile::findOrFail($user->id); 
     if(count($profile) > 0) { 
     $profile = new Profile(); 
     $profile->gender = $data['gender']; 
     $profile->city = $data['city']; 
     $profile->state = $data['state']; 
     $profile->profession = $data['profession']; 
     $profile->aboutmyself = $data['aboutmyself']; 
     $profile->fb = $data['fb']; 
     $profile->twitter = $data['twitter']; 
     $profile->gp = $data['gp']; 
     $profile->instagram = $data['instagram']; 
     $profile->personal_site = $data['personal_site']; 
     $profile->aboutme = $data['aboutme']; 
     $profile->linkedin = $data['linkedin']; 
     $profile->pinterest = $data['pinterest']; 
     //$profile = $user->profile()->save($profile); 
     $profile->save(); 
} 
    } else { 
     return redirect()->back()->withInput()->withInfo("Something went wrong. Please, try again"); 
    } 
    return redirect()->route('profile')->withSuccess("Your Profile Succesfully Updated."); 

} 

用戶遷移

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateUsersTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name'); 
      $table->string('login'); 
      $table->string('email')->nullable(); 
      $table->string('phone')->nullable(); 
      $table->string('password', 60); 
      $table->string('birthday'); 
      $table->string('country')->default('AF'); 
      $table->string('address'); 
      $table->integer('active')->default(0); 
      $table->rememberToken(); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('users'); 
    } 
} 

PROFILE遷移

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateProfilesTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('profiles', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('user_id')->unsigned(); 
      // $table->string('birthday'); 
      $table->string('aboutmyself'); 
      $table->string('gender'); 
      $table->string('age'); 
      $table->string('propic')->default('uploads/demo.png'); 
      $table->string('address'); 
      $table->string('state'); 
      $table->string('city'); 
      $table->string('fb'); 
      $table->string('twitter'); 
      $table->string('gp'); 
      $table->string('personal_site'); 
      $table->string('instagram'); 
      $table->string('aboutme'); 
      $table->string('linkedin'); 
      $table->string('pinterest'); 

      $table->foreign('user_id')->references('id')->on('users') 
       ->onUpdate('cascade')->onDelete('cascade'); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('profiles'); 
    } 
} 
+0

[將數據插入數據庫與Laravel 5]可能重複(http://stackoverflow.com/questions/40428040/inserting-data-into-database-with-laravel-5) – RST

+0

似乎你是張貼在同一主題多次。同時注意評論。在你的其他主題有人提到你提供的方式太多代碼https://stackoverflow.com/questions/40428040/inserting-data-into-database-with-laravel-5 – RST

回答

0

需要查看很多代碼,但是,我認爲您的問題可能在於您的User型號上的$fillable陣列。看來,你已經添加額外的列到你的用戶表,而允許它們被大量分配,所以你需要確保你將它們添加到$fillable陣列像這樣:

protected $fillable = ['name', 'login','email', 'password', 'country', 'birthday'...];

參見:https://laravel.com/docs/5.3/eloquent#mass-assignment

你應該也確實使用了驗證一個FormRequest這種規模,以防止它堵塞你的控制器:

https://laravel.com/docs/5.3/validation#form-request-validation

而且你應該看看如何保存的關係:

https://laravel.com/docs/5.3/eloquent-relationships#inserting-and-updating-related-models

你應該注意到,eloquent您輸入字段自動映射到數據庫列具有相同的名稱,所以你應該真的能打破這種控制方法下只需要幾行代碼。

0

您違反了外鍵約束。您不會將任何內容添加到$ profile-> user_id中,並且它保持爲空,並且您的數據庫不允許這樣做。只需在$profile = new Profile();之後添加$profile->user_id = $user->id;即可使用。

相關問題