php - Laravel更新数据库表设计

标签 php mysql laravel migration

我有一个 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/

相关文章:

php - 密码中的字符串和盐与 crypt() 相同是否安全?

java.sql.SQLException : Invalid argument value: java. io.NotSerializableException 异常

apache - 如何安装 Laravel 应用程序以免费虚拟主机?

image - 在 Laravel 中存储照片。迁移文件的结构

php - 从 1.2.7 更新到 1.3.0 的 Doctrine/注释正在减慢我的应用程序

php - 将日期转换为 unixtime php

php - 使用 PHP 从 MySQL 数据库检索 Blob 图像

mysql - 我需要一个 MySQL 子查询吗?我怎样才能得到这些结果?

javascript - 如何使用 Vuejs 在 html 中显示 mysql blob 图像?

php - Laravel Auth 自定义字段