您的位置:首页 > 编程语言 > PHP开发

[李景山php]每天laravel[034]-laravel 基础知识 --- 数据迁移及填充

2017-04-10 09:28 801 查看
1 创建数据库迁移命令文件 Migrations 文件

php artisan make:migration create_users_table


执行了这个命令,将会在 database/migrations 目录下产生

选项参数:

--table=users
--create=users
--paths=新路径


2 创建文件说明:

up方法:执行 新增表、列、索引等

down方法:跟up刚好相反的操作

例如代码:

// 创建数据库

public function up(){
Schema::create('flights',function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->string('airline');
$table->timestamps();
});
}


// 删除数据库

public function down(){
Schema::drop('flights');
}


3 执行 Migrations 文件。

命令: php artisan migrate

注意:如果这个命令报错“class not found”可以执行 composer dump-autoload

强制执行命令:
php artisan migrate -- force
命令回滚
php artisan migrate:rollback
或者
php artisan migrate:reset
先回滚,然后刷新执行
php artisan migrate:refresh
php artisan migrate:refresh --seed


4 数据库Migrations 写入命令

创建数据表

Schema:create('users',function(Blueprint $table){
$table->increments('id');
});


检测表及字段是否存在

if(Schema::hasTable('users')){
}
if(Schema::hasColumn('users','email')){
}


5 连接其它服务器,及设置相应的存储引擎

connection() 方法
Schema::connection('foo')->create();
$table->engine = 'InnoDB' 设置存储引擎


改名及 删除 表

改名:

Schema::rename($from,$to);


删除

Schema::drop('users');
Schema::dropIfExists('users');


创建各种各样的字段:修改字段:删除字段

创建、删除索引 外键
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php 数据库 迁移 数据
相关文章推荐