2016-07-22 70 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