2016-02-26 54 views
4

我正在開發一個項目,其中,我已經分配了一個任務來爲應用程序創建用戶管理。但是我堅持表格關係和他們的遷移。如何在Laravel 5中創建表遷移

努力

我有這些表:

  1. 用戶
    • USER_ID
    • 用戶名
    • 密碼
  2. 型材
    • PROFILE_ID
    • USER_ID
    • 姓氏
    • 電子郵件
  3. 地址

    • ADDRESS_ID
    • PROFILE_ID
    • 地址
    • 城市
    • 狀態
    • 國家
    • pin碼。
  4. 配置
    • config_id
    • configuration_name
    • configuration_type
    • PARENT_ID

現在我要創建模型和遷移與上述相同的結構。爲此,我在模型和遷移類下創建/修改。

型號:用戶

namespace App; 

use Illuminate\Foundation\Auth\User as Authenticatable; 

class User extends Authenticatable 
{ 
    /** 
    * The attributes that are mass assignable. 
    * 
    * @var array 
    */ 
    protected $fillable = [ 
     'username', 'password', 
    ]; 

    /** 
    * The attributes excluded from the model's JSON form. 
    * 
    * @var array 
    */ 
    protected $hidden = [ 
     'password', 'remember_token', 
    ]; 

    public function profile() 
    { 
     return $this->hasOne('Profile','user_id'); 
    } 
} 

遷移:2014_10_12_000000_create_users_table.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('user_id'); 
      $table->string('username'); 
      $table->string('password', 60); 
      $table->rememberToken(); 
      $table->timestamps(); 
     }); 
    } 

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

型號:簡介

use Illuminate\Database\Eloquent\Model; 

class Profile extends Model 
{ 
    public function user(){ 
     return $this->belongsTo('User'); 
    } 
    public function address() 
    { 
     return $this->hasOne('Address','address_id'); 
    } 
} 

移民:2016_02_26_101749_create_profiles_table。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('profile_id'); 
      $table->integer('user_id')->unsigned(); 
      $table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade'); 
      $table->string('lastname')->nullable(); 
      $table->string('firstname')->nullable(); 
      $table->string('gender')->nullable(); 
      $table->string('email')->unique(); 
      $table->string('phonenumber', 20)->nullable(); 
      $table->timestamps(); 
     }); 
    } 

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

型號:Addess

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Address extends Model 
{ 
    public function profile(){ 
     return $this->belongsTo('Profile'); 
    } 

    public function city() { 
     return $this->hasOne('Configuration', 'config_id'); 
    } 

    public function state() { 
     return $this->hasOne('Configuration', 'config_id'); 
    } 

    public function country() { 
     return $this->hasOne('Configuration', 'config_id'); 
    } 
} 

遷移:2016_02_26_102805_create_addresses_table.php

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

class CreateAddressesTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('addresses', function (Blueprint $table) { 
      $table->increments('address_id'); 
      $table->integer('profile_id')->unsigned(); 
      $table->foreign('profile_id')->references('profile_id')->on('profiles')->onDelete('cascade'); 
      $table->string('address')->nullable(); 
      $table->integer('city')->unsigned(); 
      $table->foreign('city')->references('config_id')->on('configurations')->onDelete('cascade'); 
      $table->string('pincode')->nullable(); 
      $table->integer('state')->unsigned(); 
      $table->foreign('state')->references('config_id')->on('configurations')->onDelete('cascade'); 
      $table->integer('country')->unsigned(); 
      $table->foreign('country')->references('config_id')->on('configurations')->onDelete('cascade'); 
      $table->timestamps(); 
     }); 
    } 

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

型號:配置

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Configuration extends Model 
{ 
    public function children() { 
     return $this->hasMany('Configuration','parent_id'); 
    } 
    public function parent() { 
     return $this->belongsTo('Configuration','parent_id'); 
    } 
    public function address(){ 
     return $this->belongsTo('Address'); 
    } 
} 

遷移:2016_02_26_104519_create_configurations_table.php

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

class CreateConfigurationsTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('configurations', function (Blueprint $table) { 
      $table->increments('config_id'); 
      $table->string('configuration_name'); 
      $table->string('configuration_type'); 
      $table->string('parent_id'); 
      $table->timestamps(); 
     }); 
    } 

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

現在,當我運行php artisan migrate我得到錯誤:

migration error

請告訴我該怎麼做。我必須使用相同的表結構,並且不能修改它。如果有任何進一步更新需要或我忘記了什麼,請讓我知道。

+0

我已經在遷移表中創建了user_id not id。所以,我認爲這不是問題。我認爲問題是以地址和配置爲例,地址包含城市,州,國家列,這些列將是配置表中的外鍵。 – Mandy

+0

對不起,我意識到後,我發佈了評論,你的列名是'user_id'在用戶表中 - 困惑了我一會兒。我刪除了評論。 – DavidT

+0

單獨運行遷移以查看哪個給你錯誤。 –

回答

2

這種情況發生,因爲遷移將嘗試配置之前遷移地址表,這樣就不會找到你所引用的外鍵config_id,所以你可以改變的遷移文件的名字,那麼migration commad可以先通過configurations_table遷移文件那麼addresses_table遷移文件,所以只是改變:

2016_02_26_104519_create_configurations_table.php 

要:

2016_02_26_102005_create_configurations_table.php 
_____________^ 

之後,你應該運行優化命令再生優化的類裝載機:

php artisan o 

並重新運行php artisan migrate命令現在應該解決問題。

希望這會有所幫助。

+1

是的,它解決了我的問題。你能告訴我是我的模型,他們的關係是好的。 – Mandy

+0

不確定,我第一次看到相同的列('config_id')代表不同的信息(城市,州,國家)不知道它是否會工作。 –