为 Laravel 迁移设置默认值

Setting default value for Laravel Migration

我正在使用 Laravel 5.6 并具有多重身份验证功能,并且工作正常。我的数据库中有用户和管理员 table。我如何在 admin table 上设置默认值,例如 NAME、EMAIL 和 PASSWORD?所以每次我 运行 php artisan migrate:refresh 我都会在管理员 table 中有默认的 NAME EMAIL 和 PASSWORD?谢谢!

如果不做任何澄清,您的问题有两个可能的答案。如果您询问数据库中的列是否具有默认值,这已经是 ,但只需将方法 default 链接到您的属性即可。

$table->tinyInteger('status')->default('1');

如果您询问每次迁移刷新时如何在数据库中填充一行。您可以使用 seeders:

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UsersTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'name' => 'default_name',
            'email' => 'default@mail.com',
            'password' => 'password',
        ]);
    }
}

之后只需调用:

php artisan migrate:refresh --seed