2016-11-05 18 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)) 

とコードのその部分を意味THIS

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

プロファイル移行

<?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

あなたが本当にダウンそのコントローラのメソッドを破ることができるはずですので eloquentが自動的に同じ名前のデータベース列に、あなたの入力フィールドをマップすることに注意しなければならない

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

そして、あなたは関係を保存する方法を見なければなりませんほんの数行のコードになります。

0

外部キー制約に違反します。 $ profile-> user_idに何も追加せず、nullのままであり、データベースはそれを許可しません。 $profile = new Profile();の後に$profile->user_id = $user->id;を追加するだけで動作します。

関連する問題