dongye1942
2012-10-04 21:18
浏览 103
已采纳

在Laravel迁移文件中填充数据库

I'm just learning Laravel, and have a working migration file creating a users table. I am trying to populate a user record as part of the migration:

public function up()
{
    Schema::create('users', function($table){

        $table->increments('id');
        $table->string('email', 255);
        $table->string('password', 64);
        $table->boolean('verified');
        $table->string('token', 255);
        $table->timestamps();

        DB::table('users')->insert(
            array(
                'email' => 'name@domain.com',
                'verified' => true
            )
        );

    });
}

But I'm getting the following error when running php artisan migrate:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'vantage.users' doesn't exist

This is obviously because Artisan hasn't yet created the table, but all the documentation seems to say that there is a way of using Fluent Query to populate data as part of a migration.

Anyone know how? Thanks!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

7条回答 默认 最新

  • dpjuppr1361 2012-10-04 21:25
    已采纳

    Don't put the DB::insert() inside of the Schema::create(), because the create method has to finish making the table before you can insert stuff. Try this instead:

    public function up()
    {
        // Create the table
        Schema::create('users', function($table){
            $table->increments('id');
            $table->string('email', 255);
            $table->string('password', 64);
            $table->boolean('verified');
            $table->string('token', 255);
            $table->timestamps();
        });
    
        // Insert some stuff
        DB::table('users')->insert(
            array(
                'email' => 'name@domain.com',
                'verified' => true
            )
        );
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongpo7467 2012-10-04 21:23

    try: (not tested)

    public function up()
    {
        Schema::table('users', function($table){
    
            $table->increments('id');
            $table->string('email', 255);
            $table->string('password', 64);
            $table->boolean('verified');
            $table->string('token', 255);
            $table->timestamps();
    
            $table->insert(
                array(
                    'email' => 'name@domain.com',
                    'verified' => true
                )
            );
    
        });
    }
    
    评论
    解决 无用
    打赏 举报
  • dporb84480 2012-11-21 05:11

    This should do what you want.

    public function up()
    {
        DB::table('user')->insert(array('username'=>'dude', 'password'=>'z19pers!'));
    }
    
    评论
    解决 无用
    打赏 举报
  • dongmao3131 2014-05-22 20:19

    Here is a very good explanation of why using Laravel's Database Seeder is preferable to using Migrations: http://laravelbook.com/laravel-database-seeding/

    Although, following the instructions on the official documentation is a much better idea because the implementation described at the above link doesn't seem to work and is incomplete. http://laravel.com/docs/migrations#database-seeding

    评论
    解决 无用
    打赏 举报
  • doubeng9407 2015-09-30 02:43

    I know this is an old post but since it comes up in a google search I thought I'd share some knowledge here. @erin-geyer pointed out that mixing migrations and seeders can create headaches and @justamartin countered that sometimes you want/need data to be populated as part of your deployment.

    I'd go one step further and say that sometimes it is desirable to be able to roll out data changes consistently so that you can for example deploy to staging, see that all is well, and then deploy to production with confidence of the same results (and not have to remember to run some manual step).

    However, there is still value in separating out the seed and the migration as those are two related but distinct concerns. Our team has compromised by creating migrations which call seeders. This looks like:

    public function up()
    {
        Artisan::call( 'db:seed', [
            '--class' => 'SomeSeeder',
            '--force' => true ]
        );
    }
    

    This allows you to execute a seed one time just like a migration. You can also implement logic that prevents or augments behavior. For example:

    public function up()
    {
        if ( SomeModel::count() < 10 )
        {
            Artisan::call( 'db:seed', [
                '--class' => 'SomeSeeder',
                '--force' => true ]
            );
        }
    }
    

    This would obviously conditionally execute your seeder if there are less than 10 SomeModels. This is useful if you want to include the seeder as a standard seeder that executed when you call artisan db:seed as well as when you migrate so that you don't "double up". You may also create a reverse seeder so that rollbacks works as expected, e.g.

    public function down()
    {
        Artisan::call( 'db:seed', [
            '--class' => 'ReverseSomeSeeder',
            '--force' => true ]
        );
    }
    

    The second parameter --force is required to enable to seeder to run in a production environment.

    评论
    解决 无用
    打赏 举报
  • douren7921 2018-12-20 16:03

    Another clean way to do it is to define a private method which create instance et persist concerned Model.

    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('label', 256);
            $table->timestamps();
            $table->softDeletes();
        });
    
        $this->postCreate('admin', 'user');
    }
    
    private function postCreate(string ...$roles)  {
        foreach ($roles as $role) {
            $model = new Role();
            $model->setAttribute('label', $role);
            $model->save();
        }
    }
    

    With this solution, timestamps fields will be generated by Eloquent.

    EDIT: it's better to use seeder system to disctinct database structure generation and database population.

    评论
    解决 无用
    打赏 举报
  • dongxu4580 2019-08-24 10:24

    I tried this DB insert method, but as it does not use the model, it ignored a sluggable trait I had on the model. So, given the Model for this table exists, as soon as its migrated, I figured the model would be available to use to insert data. And I came up with this:

    public function up() {
            Schema::create('parent_categories', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->string('slug');
                $table->timestamps();
            });
            ParentCategory::create(
                [
                    'id' => 1,
                    'name' => 'Occasions',
                ],
            );
        }
    

    This worked correctly, and also took into account the sluggable trait on my Model to automatically generate a slug for this entry, and uses the timestamps too. NB. Adding the ID was no neccesary, however, I wanted specific IDs for my categories in this example. Tested working on Laravel 5.8

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题