2016-07-22 62 views
1

我有一個php laravel項目,我需要添加一個字段到一個/多個模型(雄辯)。我沒有太多的PHP經驗,從來沒有嘗試過laravel。Laravel更新數據庫表設計

這個類是現在這個樣子

class Player extends Eloquent 
{ 
use GenderTrait; 
use VisibilityTrait; 
use PlayerPhotoTrait; 
use PlayerActionTrait; 

const GENDER_MALE = 2; 
const GENDER_FEMALE = 1; 

/** 
* The database table used by model. 
* 
* @var string 
*/ 
protected $table = 'players'; 

/** 
* Parameters for `actions` relation. 
* 
* @see PlayerActionTrait::actions() 
* @var array 
*/ 
protected $actionModel = [ 
    'name' => 'PlayerAction', 
    'foreignKey' => 'player_id', 
]; 

/** 
* The list of mass-assignable attributes. 
* 
* @var array 
*/ 
protected $fillable = [ 
    'name', 
    'start_value', 
    'gender', 
    'is_visible', 
    'nation', 
]; 

/** 
* The list of validation rules. 
* 
* @var array 
*/ 
public static $rules = [ 
    'name' => 'required', 
    'nation' => 'required|numeric', 
    'start_value' => 'required|numeric', 
]; 

/** 
* @inheritdoc 
*/ 
protected static function boot() 
{ 
    parent::boot(); 

} 

/** 
* Players country. 
* 
* @return Country 
*/ 
public function country() 
{ 
    return $this->belongsTo('Country', 'nation'); 
} 

/** 
* Player videos. 
* 
* @return mixed 
*/ 
public function videos() 
{ 
    return $this->morphMany('YoutubeLink', 'owner'); 
} 
} 

我想添加一個名爲「水平」的字符串場,但我不知道如何去做。如果我先在MySQL中創建字段,然後模型得到更新,如果我更新模型,然後Laravel爲我更新MySQL?

進出口期待聽到我能做些什麼:)

+0

您使用的是遷移嗎?如果不是,我強烈建議你這樣做https://laravel.com/docs/5.2/migrations –

+0

這裏有用的鏈接:http://stackoverflow.com/questions/16791613/add-new-column-migrate-to-database –

+0

是的,該項目正在使用遷移,但我只是剛剛從別人那裏接管了該項目 – Alpo

回答

1

您需要添加遷移:

​​

公開賽在/database/migrations新的遷移和寫入

Schema::table('players', function ($table) { 
    $table->string('new_string_field'); 
}); 

現在,您需要運行遷移

php artisan migrate 

更多信息和可用列類型here

+0

您是否知道這是否可以在one.com服務器上進行? – Alpo

+0

one.com提供了ssh訪問權限,所以你可以在你的服務器上運行命令 –

+0

好的,謝謝,我會在有機會的時候嘗試。運行最後一個命令後,我的Players表格+模型會自動更新嗎? – Alpo