首页 技术 正文
技术 2022年11月11日
0 收藏 648 点赞 3,305 浏览 1414 个字

声明:

本教程参考Jeffrey way 在laracasts.com上的视频教程,感谢Jeffrey way为大家带来的精彩教程,本教程如有侵权,请及时告知,联系邮箱https://www.shuzhiduo.com/A/kjdwxqYjzN/wanglv93@gmail.com

Jeffery 视频地址:https://laracasts.com/series/laravel-5-fundamentals

1、首先使用php artisan的migration来创建数据库

打开命令行,输入

“`php

php artisan make:migration create_articles –create=”articles”

“`

–create 表示这条migration将创建的表名是articles

打开刚刚创建的database/migrations/XXXXXXX_create_articles.php

修改文件代码为:

public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
$table->timestamp('published_at');
});
}
下面执行迁移命令,让这个文件生成数据库
php artisan migrate
打开数据库,我们会看到生成了我们要的文章表

我们继续学习一下migration,现在我们已经有了一个articles表,但是我们想要在这个表里加一个字段,这个时候,我们只需要再创建一个migration,命令如下:

php artisan make:migration add_excerpt_to articles_table –table=”articles”

–table 也是一个可选参数,加上这个参数说明,你想操作的表示articles这个表。

打开这个文件,修改里边代码:

    public function up()
{
Schema::table('articles', function (Blueprint $table) {
$table->text('excerpt');
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('articles', function (Blueprint $table) {
$table->dropColumn('excerpt');
});
}
老样子,还是使用迁移命令,让这个文件对数据库操作生效
php artisan migrate

articles表多出一个字段吧

one more thing ..

这个时候我想执行撤销功能,不想添加这个字段了,即想调用add_excerpt_to articles_table 的down方法

我只需要执行:php artisan migrate:rollback

就会撤销刚才的操作

注意:在撤销操作是如果发生错误(laravel5.1是没有问题的,在5.0版本可能有问题)

可能是你的laravel缺少一个包,安装一下就好了

composer require doctrine/dbal

至此,我们的数据库已经完成文章表的创建了!

上一篇: pods的问题处理
下一篇: MSSQL-资源地址
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,075
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,551
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,399
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,811
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,892