本文介绍了无法在Laravel中播种数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Laravel中植入数据库,迁移工作正常,并且能够在SQL工作台中看到表.但是,当我运行命令 php artisan db:seed 时,没有任何反应.

I'm currently seed my database in Laravel, migrations works properly and I am able to see the tables in SQL workbench. But when I run the command php artisan db:seed nothing happens.

我找不到原因.我是Laravel的新手.

I can't find the cause of it. I'm pretty new to Laravel.

数据库名称为"Laravel",表名称为"books".

DB name is 'Laravel', the table name is 'books'.

播种者代码:

 DB::table('books')->insert($books = [
        ['name' => 'Harry Potter', 'writer_name' => 'J.K. Rowling', 'isbn' => '9780739360385'],
        ['name' => 'Game of Thrones', 'writer_name' => 'George R.R. Martin', 'isbn' => '9780739308684'],
        ['name' => 'Harry Potter', 'writer_name' => 'J.R.R. Tolkien', 'isbn' => '9780563528807'],
        ['name' => 'The Lord of The Rings', 'writer_name' => 'J.R.R. Tolkien', 'isbn' => '9780563528883'],
        ['name' => 'The Silmarillion', 'writer_name' => 'J.R.R. Tolkien', 'isbn' => '9780007120604'],
        ['name' => 'Animal Farm', 'writer_name' => 'George Orwell', 'isbn' => '9780140862515'],
        ['name' => 'It', 'writer_name' => 'Stephan King', 'isbn' => '9781441738707'],
        ['name' => 'The Art of Deception', 'writer_name' => 'Kevin Mitnick', 'isbn' => '9780470249321'],
    ]);
    foreach ($books as $book) {
        Book::create($book);
    }

迁移代码:

public function up()
{
    Schema::create('books', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('writer_name');
        $table->string('image')->nullable();
        $table->string('isbn')->unique();
        $table->timestamps();
    });

推荐答案

DatabaseSeeder 类中,您可以使用 call 方法执行其他种子类.使用 call 方法,您可以将数据库种子分解为多个文件,这样就不会有单个种子类变得过大.传递您希望运行的种子类的名称:

Within the DatabaseSeeder class, you may use the call method to execute additional seed classes. Using the call method allows you to break up your database seeding into multiple files so that no single seeder class becomes overwhelmingly large. Pass the name of the seeder class you wish to run:

/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    $this->call([
        UserSeeder::class,
        PostSeeder::class,
        CommentSeeder::class,
    ]);
}

这篇关于无法在Laravel中播种数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 16:36