laravel中数据库迁移的使用:

创建数据库迁移文件:

php artisan make:migration create_links_table

创建完表之后,设置字段:

 public function up()
    {
        Schema::create('links', function (Blueprint $table) {
            $table->engine="MyISAM";
            $table->increments('links_id');
            $table->string("links_name")->default("")->comment("友情链接的名字");
            $table->string("links_title")->default("")->comment("友情链接的标题");
            $table->string("links_url")->default("")->comment("友情链接的url");
            $table->integer("link_order")->default(0)->comment("友情链接排序");
        });
    }

    public function down()
    {
        Schema::dropIfExists('links');
    }

设置完字段之后,在运行数据库迁移文件:

php artisan migrate

为创建的数据库迁移文件创建的表填充数据:

创建数据填充文件:

php artisan make:seeder LinksTableSeeder

在文件中填充数据:

 public function run()
    {
        //
        $data=[
            [
                'links_name'=>"百度",
                'links_title'=>"百度一下就知道",
                'links_url'=>"https://www.baidu.com",
                'links_order'=>1,
            ],
            [
                'links_name'=>"新浪",
                'links_title'=>"新浪知天下",
                'links_url'=>"https://www.sina.com",
                'links_order'=>2,
            ]
        ];
        DB::table('links')->insert($data);
    }

  在DtabaseSeeder中填写:

    public function run()
    {
        $this->call(LinksTableSeeder::class);
    }

  

运行:

php artisan db:seed

原文地址:https://www.cnblogs.com/yiweiyihang/p/8215264.html