zoukankan      html  css  js  c++  java
  • laravel框架总结(十四) -- 数据迁移和数据填充

    一.数据迁移

    1.创建一个迁移

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

      php artisan make:migration create_sutdents_table

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

      --table用来指定表名

        php artisan make:migration create_students_table --table=students

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

        php artisan make:migration create_students_table --create=students

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

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

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

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

      

    三个迁移文件的主要内容如下,感受下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 IlluminateDatabaseSeeder;
    
    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 IlluminateDatabaseSeeder;
    use IlluminateDatabaseEloquentModel;
    
    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 这个辅助函数函数来添加数据到数据库。

  • 相关阅读:
    vue form dynamic validator All In one
    TypeScript api response interface All In One
    closable VS closeable All In One
    macOS 如何开启 WiFi 热点 All In One
    vue css inline style All In One
    vs2010里面 新建网站里面的 asp.net网站 和 新建项目里面的 asp.net Web应用程序 的区别 (下)
    牛腩新闻 59 整合添加新闻页 FreeTextBox 富文本编辑器,检测到有潜在危险的 Request.Form 值,DropDownList 的使用
    牛腩新闻 61尾声: error.aspx的使用 防止报错
    vs2010里面 新建网站里面的 asp.net网站 和 新建项目里面的 asp.net Web应用程序 的区别 (上)
    牛腩新闻 62:尾声续2 asp.net的编译和发布
  • 原文地址:https://www.cnblogs.com/ghjbk/p/6638138.html
Copyright © 2011-2022 走看看