laravel框架总结(十四) -- 数据迁移和数据填充

一.数据迁移

1.建立一个迁移

1>使用artisan命令make:migration来建立一个新的迁移:  php

  php artisan make:migration create_sutdents_table数据库

新的迁移位于database/migrations目录下,每一个迁移文件名都包含时间戳从而容许Laravel判断其顺序。
2>其余一些选项

  --table用来指定表名app

    php artisan make:migration create_students_table --table=students框架

  --create建立一个新的数据表(有表名和基本字段)dom

    php artisan make:migration create_students_table --create=students函数

  --path选项用来自定义输出路径post

    php artisan make:migration create_students_table --path=app/migrations学习

  指定生成迁移的自定义输出路径,在执行make:migration命令时可使用--path选项,提供的路径应该是相对于应用根目录的。this

  下面是我生成迁移文件的情形spa

  

三个迁移文件的主要内容以下,感觉下up和down方法中内容的不一样
//1>php artisan make:migration create_sutdents_table中的内容 public function up() { // } public function down() { // } //2>php artisan make:migration create_sutdents_table --table=students中的内容 public function up() { Schema::table('students', function (Blueprint $table) { // }); } public function down() { Schema::table('students', function (Blueprint $table) { // }); } //3>php artisan make:migration create_sutdents_table --create=students中的内容 public function up() { Schema::create('students', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } public function down() { Schema::drop('students'); }

2.迁移结构

迁移类包含了两个方法:up和down。up方法用于新增表,列或者索引到数据库,而down方法就是up方法的反操做,和up里的操做相反。

关于具体如何编辑前一结构,能够参见官方文档

3.实施迁移

在控制台执行如下命令,便可执行迁移文件,生成或更新相应的表。

  php artisan migrate

4.回滚迁移

1>回滚上一次的迁移

php artisan migrate:rollback

2>回滚全部迁移

php artisan migrate:reset

3> 回滚全部迁移而且再执行一次

php artisan migrate:refresh

5.其它

1>使用help来查看信息

  <1>php artisan help make:migration来查看更多的选项

    

<2>php artisan help migrate
  
<3>php artisan help migrate:refresh
  
 

2>咱们有多个迁移文件的时候,运行迁移命令后,是否全部文件都会执行

  咱们如今运用基本的迁移 php artisan migrate

    第一次运行,显示:tabale created successfully

    第二次运行,显示:nothing to migrate

  由于咱们的数据库已经迁移成功没有变化了,因此不会再执行上次的文件

  接下来咱们运行回滚操做 php artisan migrate:rollback

    第一次运行,显示:回滚文件信息

    第二次运行,显示:nothing to rollback

  一样,已经没有能够回滚的信息了,不会重复执行

  

3>php artisan migrate:refresh操做
  
 

二.数据填充

1.生成一个seeder文件

  你能够经过 make:seeder artisan命令来生成一个 Seeder。全部经过框架生成的 Seeder 都将被放置在 database/seeds 路径:

    php artisan make:seeder StudentsTableSeeder

  在 seeder 类里只有一个默认方法:run。咱们能够在这个run方法中给数据库添加任何数据(在这里咱们可使用查询构造器或者Eloquent模型工厂)

  咱们给run方法添加以下内容

<?php

use Illuminate\Database\Seeder; class StudentsTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run(){ DB::table('students')->insert([ 'name' => str_random(10), ]); } } 

  而后咱们运行命令,调用StudentsTableSeeder类 php artisan db:seed --class=StudentsTableSeeder

  咱们能够发现数据表中新插入了一条数据

  这里要注意一个问题,由于我设计数据库的时候增长了created_at和updated_at字段,使用查询构造器并不能自动更新这两个字段

2.Laravle 默认为你定义了一个 DatabaseSeeder 类。

  你能够在这个类中使用 call 方法来运行其它的 seed 类,以借此控制数据填充的顺序。

  咱们将咱们生成的StudentsTableSeeder类加入到 run方法中

<?php

use Illuminate\Database\Seeder; use Illuminate\Database\Eloquent\Model; class DatabaseSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run(){ Model::unguard(); // $this->call(UserTableSeeder::class); //这里咱们能够调用更多生成的填充数据类,便于管理 $this->call(StudentsTableSeeder::class); Model::reguard(); } } 

  咱们运行命令,调用DatabaseSeeder类

    php artisan db:seed

  咱们一样能够发现数据表中多了一条数据

3.咱们刚才在run方法中使用的时候查询构造器,咱们还可使用Eloquent模型工厂

  首先,学习如何定义你的工厂,一旦工厂被定义(位于database/fatories文件夹下),就能使用 factory 这个辅助函数函数来添加数据到数据库。

相关文章
相关标签/搜索