php – 在保留测试数据的同时在laravel 5.2迁移中添加字段的过程是什么?

php – 在保留测试数据的同时在laravel 5.2迁移中添加字段的过程是什么?,第1张

概述我正在学习并尝试使用laravel 5.2,我很困惑如何在表格中添加字段. 我创建了一个名为2016_01_29_093417_create_thread_table.php的迁移文件,每当我想添加一个字段时,我在文件中添加一个代码,例如 $table->string('content'); 然后运行该命令 php artisan migrate:refresh 新字段将出现在表中但测试数据(例如 我正在学习并尝试使用laravel 5.2,我很困惑如何在表格中添加字段.

我创建了一个名为2016_01_29_093417_create_thread_table.PHP的迁移文件,每当我想添加一个字段时,我在文件中添加一个代码,例如

$table->string('content');

然后运行该命令

PHP artisan migrate:refresh

新字段将出现在表中但测试数据(例如用户表中的用户将被截断)

问题是:

1)向表中添加新字段的正确方法(最佳实践)是什么?
2)如何将测试数据保存在所有表中,例如用户表中的用户?

谁知道该怎么办?

解决方法 有两种方法可以做到这一点.

您的应用程序已经与使用它的人一起使用,并且您不想丢失其数据:您只需进行新的迁移并使用相应的 *** 作提供up()和down().例如:

<?PHPuse Illuminate\Database\Schema\Blueprint;use Illuminate\Database\Migrations\Migration;class UsersNewFIEld extends Migration{    /**     * Run the migrations.     *     * @return voID     */    public function up()    {        Schema::table('users',function ($table) {            $table->string('email');        });    }    /**     * Reverse the migrations.     *     * @return voID     */    public function down()    {        Schema::table('users',function ($table) {            $table->dropColumn('email');        });    }}

运行PHP artisan migrate后,它将只运行最新的迁移.

您的应用程序仍在开发中,还没有人使用它:您使用种子来填充数据库中的测试数据并根据需要编辑初始迁移.例如:

<?PHPuse Illuminate\Database\Seeder;class UseRSSeeder extends Seeder{    /**     * Run the database seeds.     *     * @return voID     */    public function run()    {        DB::table('users')->insert([            'name' => 'First User','email' => '[email protected]','password' => bcrypt('somepass9019'),]);        DB::table('users')->insert([            'name' => 'Second User','email' => '[email protected]','password' => bcrypt('somepass2039'),]);        DB::table('users')->insert([            'name' => 'Third User','email' => '[email protected]','password' => bcrypt('somepass0534'),]);    }}

运行PHP artisan migrate:refresh –seed后,它将重置数据库并使用初始/测试数据对其进行播种.

总结

以上是内存溢出为你收集整理的php – 在保留测试数据的同时在laravel 5.2迁移中添加字段的过程是什么?全部内容,希望文章能够帮你解决php – 在保留测试数据的同时在laravel 5.2迁移中添加字段的过程是什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1256415.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存