Laravel Migrate
来源:互联网 发布:Python 线程锁 编辑:程序博客网 时间:2024/06/07 02:20
artisan命令行创建migrate
格式:
1
php artisan
make
:migration YourFileName
示例:
1
php artisan
make
:migration create_books_table
我们找到laravel目录下database\migrations\2017_XX_XX_XXXXXX_create_books_table.php
注意:XX代表时间戳,因时而异
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
use
Illuminate\Database\Schema\Blueprint;
use
Illuminate\Database\Migrations\Migration;
class
CreateBooksTable
extends
Migration
{
/**
* Run the migrations.
*
* @return void
*/
public
function
up()
{
//
}
/**
* Reverse the migrations.
*
* @return void
*/
public
function
down()
{
//
}
}
这里的CreateBooksTable类继承了Migration,我们看下Migration类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
namespace
Illuminate\Database\Migrations;
abstract
class
Migration
{
/**
* The name of the database connection to use.
*
* @var string
*/
protected
$connection
;
/**
* Get the migration connection name.
*
* @return string
*/
public
function
getConnection()
{
return
$this
->connection;
}
}
这里我们把up函数改写成
1
2
3
4
5
6
7
8
9
10
11
public
function
up()
{
Schema::create(
'chs'
,
function
(Blueprint
$table
) {
$table
->increments(
'id'
);
$table
->string(
'slug'
)->unique();
//additional
$table
->string(
'title'
);
//additional
$table
->text(
'content'
);
//additional
$table
->timestamps();
$table
->timestamp(
'published_at'
)->index();
//additional
});
}
同时参考以下文档,文档选自http://laravelbook.com/laravel-migrations-managing-databases/
一切准备就绪,我们开始迁移
1
php artisan migrate
如果你迁移后发觉并不是你想要的数据表,可以回滚
1
php artisan migrate:rollback
这里要给大家提个醒,down方法一定要写,可能有些人这个方法会空着,觉得只要数据表创建出来了就没事了,但是一旦涉及rollback又没有down方法,你的migrate操作就是不可逆的!
就像这样:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
use
Illuminate\Database\Schema\Blueprint;
use
Illuminate\Database\Migrations\Migration;
class
CreateCommentsTable
extends
Migration
{
/**
* Run the migrations.
*
* @return void
*/
public
function
up()
{
Schema::create(
'comments'
,
function
(Blueprint
$table
) {
$table
->increments(
'id'
);
$table
->integer(
'postid'
);
$table
->string(
'title'
);
//additional
$table
->text(
'content'
);
//additional
//$table->timestamps();
$table
->timestamp(
'published_at'
)->index();
//additional
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public
function
down()
{
Schema::drop(
'comments'
);
}
}
Laravel使用了门面模式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace
Illuminate\Support\Facades;
/**
* @see \Illuminate\Database\Schema\Builder
*/
class
Schema
extends
Facade
{
/**
* Get a schema builder instance for a connection.
*
* @param string $name
* @return \Illuminate\Database\Schema\Builder
*/
public
static
function
connection(
$name
)
{
return
static
::
$app
[
'db'
]->connection(
$name
)->getSchemaBuilder();
}
/**
* Get a schema builder instance for the default connection.
*
* @return \Illuminate\Database\Schema\Builder
*/
protected
static
function
getFacadeAccessor()
{
return
static
::
$app
[
'db'
]->connection()->getSchemaBuilder();
}
}
阅读全文
0 0
- Laravel Migrate
- laravel 利用migrate 创建数据表
- Laravel 创建指定表 migrate
- Laravel数据库迁移文件migrate使用约定
- laravel 执行migrate出现 1071异常
- Laravel 5.4 php artisan migrate 报错
- 遇见Laravel Migrations的migrate与rollback
- laravel 中 使用migrate对字段的删除 修改
- Laravel在Mac下使用php artisan migrate命令出现No such file or directory
- Laravel 5.1 超级大坑 CentOS 7 文件路径大小写敏感造成的Migrate异常
- Laravel:php artisan migrate报错SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default
- Laravel 5.4 migrate时报错: Specified key was too long error
- Laravel 5.4 migrate时报错: Specified key was too long error
- Laravel中数据迁移执行php artisan migrate 报错问题
- Laravel 5.4 migrate时报错: Specified key was too long error
- laravel migrate 时出现错误 [PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key
- Laravel migrate时报错: Syntax error or access violation: 1071 Specified key was too long
- Laravel 5.4 migrate时报错: Specified key was too long error
- 关于removeCallbacks不能移除消息的问题
- mac上使用Aria2,完全摆脱百度云盘的限速10kb/s
- 跨域问题的解决
- 5个必须掌握的maven命令
- 魅族虚拟按键遮挡问题
- Laravel Migrate
- Laravel控制器和视图
- sendmail 简单配置zabbix
- Laravel路由和控制器的绑定
- Web.xml详解
- CodeM美团点评编程大赛资格赛B题
- [有图有真相]帮你看懂 iPhone 6 Plus 的屏幕分辨率
- JS迭代器之for-in(of)
- IO流