我有一个 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?
我很期待听到我能做些什么 :)
最佳答案
您需要添加迁移:
php artisan make:migration add_fields_to_players_table --table=players
在/database/migrations
中打开新建migration并写入
Schema::table('players', function ($table) {
$table->string('new_string_field');
});
现在你需要运行迁移
php artisan migrate
更多信息和可用的列类型 here
关于php - Laravel更新数据库表设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38526537/