【发布时间】:2016-11-26 08:42:27
【问题描述】:
我有一个 php laravel 项目,我需要在其中向一个/多个模型(Eloquent)添加一个字段。我对php没有太多经验,之前从未尝试过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');
}
}
我想添加一个名为“level”的字符串字段,但我不知道该怎么做。如果我先在 MySQL 中创建字段然后更新模型,如果我更新模型然后 Laravel 为我更新 MySQL?
我期待听到我能做些什么:)
【问题讨论】:
-
你在使用迁移吗?如果不是,我强烈建议你这样做laravel.com/docs/5.2/migrations
-
这里有一个有用的链接:stackoverflow.com/questions/16791613/…
-
是的,该项目正在使用迁移,但我刚刚从其他人那里接管了该项目
标签: php mysql laravel migration