2017-05-31 26 views
0

我使用Laravel 5.2和MySQL。Laravel - 表外鍵不工作--Updated

我開發習慣Laravel項目。我的項目是一個電話簿,您可以在該表中存儲聯繫信息。但要做到這一點,你需要在系統中,這是我使用make:auth命令做出記錄。看起來很簡單。

在我的「Users」表我有場ID。此表用於存儲用戶信息,以便他們可以登錄並訪問聯繫人信息。

在我的「Contacts」表,這是我存放我的聯繫人,是一個名叫列「Created By」是應該採取實地ID從用戶表,只是引用誰是創造者說接觸。

但這裏是東西:

Contacts表不遷移,沒關係我做什麼。

我已經放棄了這兩個表,並且從零開始創建它們,首先是USERS,因爲它有引用的主鍵,然後是CONTACTS,並設置了外部字段。我甚至放棄我的模型,並使用上述相同的順序,因爲誰知道什麼可能的工作創造了他們。

這裏是我的移民文件:

USERS遷移

----- ... ----- 

public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name'); 
      $table->string('email', 191)->unique(); 
      $table->string('password', 191); 
      $table->timestamp('updated_at'); 
      $table->timestamp('created_at'); 
      $table->rememberToken(); 
     }); 
    } 

----- ... ----- 

如前所述,該表中的ID字段,我在我的其他表引用。

聯繫人遷移

----- ... ----- 

public function up() 
    { 
     Schema::create('contacts', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('user_id', false, true); 
      $table->foreign('user_id')->references('id')->on('users'); 
      $table->string('image'); 
      $table->string('name'); 
      $table->string('lastname'); 
      $table->string('email', 191)->unique(); 
      $table->string('phone',191)->unique(); 
      $table->string('address'); 
      $table->string('description', 255); 
     }); 

----- ... ----- 

,現場USER_ID引用id字段的表的用戶作爲說明。第二個參數是將遞增設置爲false,第三個參數設置爲unsigned爲true。即使我做了默認樣式($ table-> integer('user_id') - > unsigned(); --- $ table-> foreign .....;),遷移也不起作用。

我還送洋場的分離模式,從這個主體,像這樣:

Schema::table('contacts', function($table) { 
      $table->foreign('user_id')->references('id')->on('users'); 
     }); 

但即使這樣,遷移不工作。

我真的不知道發生了什麼。

這是我的聯繫型號

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Contact extends Model 
{ 
    // Getting the user associated with the contact 
    // which means, I hope, 
    // The user that created the contact entry in the table 'contacts' 
    public function user() 
    { 
     return $this->belongsTo('App\User'); 
    } 

    // protected $table = 'contacts'; 

    // not using an id field 
    // could this be the error? 
    // dunno 
    // isn't laravel making an automatic id field? 
    // dunno too 
    public $fillable = array('name', 'lastname', 'email', 'phone', 'address'); 

    // public $foreign = ('user_id'); 

    //added because of error at migration 
    public $timestamps = false; 


} 

這是我的用戶模型

<?php 

namespace App; 

use Illuminate\Foundation\Auth\User as Authenticatable; 

class User extends Authenticatable 
{ 
    // Putting it in simple terms 
    // Hopefully, this will return 
    // the many contacts that said user recorded in the 'contacts' table 
    public function contact() 
    { 
     return $this->hasMany('App\Contact'); 
    } 

    // protected $table = 'users'; 
    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'name', 'email', 'password', 
    ]; 

    /** 
    * The attributes that should be hidden for arrays. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 
} 

UPDATE

我只是回滾我的遷移和再次使用migrate命令,它這次工作。

我可以註冊一個用戶,所以auth::check的作品,我可以看到與表格插入CONTACTS表中的聯繫人的看法。

但是當我點擊按鈕保存,我得到以下錯誤:

2/2 

QueryException in Connection.php line 729: 
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`phonebook`.`contacts`, CONSTRAINT `contacts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) 


1/2 

PDOException in Connection.php line 457: 
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`phonebook`.`contacts`, CONSTRAINT `contacts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)) 

我仍然不知道是怎麼回事。

+0

你有什麼具體的錯誤? – Rahul

+0

在帖子Rahul中插入錯誤。 –

+0

您可以檢查您是否將現有用戶標識添加到要保存的模型中?並且請將'$ table-> integer('user_id',false,true);'改成'$ table-> integer('user_id',false,true) - > unsigned();' –

回答

1

這些是您可以添加聯繫人並附加用戶的一些方法。既然你解決了外鍵問題,我不會解決這個問題。

$user = \Auth::user(); 

$user->contact()->create([ 
    'name' => $request->input('name'), 
    'lastname' => $request->input('lastname'), 
    'email' => $request->input('email'), 
    'phone' => $request->input('phone'), 
    'address' => $request->input('address') 
]); 

或者

\App\Contact::create([ 
    'name' => $request->input('name'), 
    'lastname' => $request->input('lastname'), 
    'email' => $request->input('email'), 
    'phone' => $request->input('phone'), 
    'address' => $request->input('address'), 
    'user_id' => \Auth::id() 
]); 

或者

$contact = new \App\Contact; 

$contact->name = $request->input('name'); 
$contact->lastname = $request->input('lastname'); 
$contact->email = $request->input('email'); 
$contact->phone = $request->input('phone'); 
$contact->address = $request->input('address'); 
$contact->user_id = \Auth::id(); 

$contact->save(); 

同時添加user_id$fillable屬性時,你的質量分配值(如上所示第二個選項)。

+0

謝謝@Sandeesh!第三個選項適用於我。 –