我有一个php laravel projekt,我需要在一个/多个模型中添加一个字段(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?
我期待听到我能做些什么:)
答案 0 :(得分:2)
您需要添加迁移:
php artisan make:migration add_fields_to_players_table --table=players
在/database/migrations
新迁移中打开并撰写
Schema::table('players', function ($table) {
$table->string('new_string_field');
});
现在您需要运行迁移
php artisan migrate
更多信息和可用的列类型here