深圳网帮你
laravel 数据迁移
时间:2018-04-22 02:09:05 浏览:212

1 1、命令:
2
3 ASUS@huangxiaoshan MINGW64 /e/phpStudy/WWW/blog
4
5 ${content}nbsp;php artisan make:migration create_articles_table --create=articles
6
7 Created Migration: 2017_11_23_112358_create_articles_table
8
9 2、此时, database/migrations 目录中会多出一个***_create_articles_table.php文件,文件内容如下:
10
11
12
13
14 use Illuminate\Support\Facades\Schema;
15
16 use Illuminate\Database\Schema\Blueprint;
17
18 use Illuminate\Database\Migrations\Migration;
19
20
21 class CreateArticlesTable extends Migration {
22
23
24    /**
25
26     * Run the migrations.
27
28     *
29
30     * @return void
31
32     */
33
34    public function up() {
35
36        Schema::create('articles', function (Blueprint $table) {
37
38            $table->increments('id');
39
40            $table->timestamps();
41
42            up() 方法中创建了一个自增ID,其中的 timestamps() 方法会生成 created_at 和 updated_at 两个时间列。
43
44        });
45
46    }
47
48
49    /**
50
51     * Reverse the migrations.
52
53     *
54
55     * @return void
56
57     */
58
59    public function down() {
60
61        Schema::dropIfExists('articles');
62
63    }
64
65
66 }
67
68 3、执行迁移 php artisan migrate # 此命令按生成文件的时间顺序执行,如果被执行过了,就不执行了。
69
70 4、查看数据库,里面已经生成了相关数据表。
71
72 5、想在表article里面新增一个字段如何
73
74 a、执行命令
75
76 ASUS@huangxiaoshan MINGW64 /e/phpStudy/WWW/blog
77
78 ${content}nbsp;php artisan make:migration add_excerpt_to_articles_table --table=articles
79
80 Created Migration: 2017_11_23_113826_add_excerpt_to_articles_table
81
82
83 ASUS@huangxiaoshan MINGW64 /e/phpStudy/WWW/blog
84
85 $
86
87 b、文件内容如下:
88
89
90
91
92 use Illuminate\Support\Facades\Schema;
93
94 use Illuminate\Database\Schema\Blueprint;
95
96 use Illuminate\Database\Migrations\Migration;
97
98
99 class AddExcerptToArticlesTable extends Migration {
100
101
102    /**
103
104     * Run the migrations.
105
106     *
107
108     * @return void
109
110     */
111
112    public function up() {
113
114        Schema::table('articles', function (Blueprint $table) {
115
116            // 手动新增
117
118            $table->text('excerpt')->nullable();
119
120        });
121
122    }
123
124
125    /**
126
127     * Reverse the migrations.
128
129     *
130
131     * @return void
132
133     */
134
135    public function down() {
136
137        Schema::table('articles', function (Blueprint $table) {
138
139            //
140
141            $table->dropColumn('excerpt');
142
143        });
144
145    }
146
147
148 }
149
150 c、再次执行迁移,刷新数据库发现新增了字段。


[上一篇] laravel the bootstrap/cache di
[下一篇]全网通3.0什么意思 ?
Copyright 2025 © wbnsp.com.cn 网帮你

2025 © 版权所有 红花岗区网帮你信息技术工作室

工信部备案号:黔ICP备2025055589号-1

请使用网帮你微信扫码登录