使用Laravel5.5怎么创建表与修改表-创新互联
本篇文章为大家展示了使用Laravel5.5 怎么创建表与修改表,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

创建迁移
方法一:创建数据模型的时候加上 -m ,例如:
php artisan make:model Models/Moment -m
就会在 database/migrations 下看到新建的迁移文件。打开文件,主要有 up 和 down 方法。
当我们运行迁移时,up 方法会被调用;
当我们回滚迁移时,down 方法会被调用。
方法二:
php artisan make:migration create_moments_table
其中 “moments” 就是要创建的表名,这里要写你自己的表名。
创建表
在up方法中,我们编写创建表的语句:
Schema::create('moments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->string('content');
$table->timestamps();
});保存后执行 php artisan migrate ,会创建5个字段的article表。
修改表
方法一:修改迁移文件,执行命令
php artisan migrate:refresh
方法二:如果要修改表,新建一个迁移文件:
php artisan make:migration alter_moments_table
其中 “moments” 就是要修改的表名,这里要写你自己的表名。
在 up 方法中:
Schema::table('moments', function (Blueprint $table) {
$table->string('test');
});与创建表的区别是,create 方法改成 table 方法。
上述内容就是使用Laravel5.5 怎么创建表与修改表,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。
网站标题:使用Laravel5.5怎么创建表与修改表-创新互联
URL地址:http://www.jxjierui.cn/article/cssodh.html


咨询
建站咨询
