Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';t在Laravel中为数据库添加种子_Php_Laravel - Fatal编程技术网

Php Can';t在Laravel中为数据库添加种子

Php Can';t在Laravel中为数据库添加种子,php,laravel,Php,Laravel,我目前正在Laravel中为我的数据库播种,迁移工作正常,并且我能够在SQL workbench中查看表。但是当我运行命令phpartisan db:seed时,什么也没有发生 我找不到原因。我对拉威尔是个新手 数据库名为“Laravel”,表名为“books” 播种机代码: DB::table('books')->insert($books = [ ['name' => 'Harry Potter', 'writer_name' => 'J.K. Rowli

我目前正在Laravel中为我的数据库播种,迁移工作正常,并且我能够在SQL workbench中查看表。但是当我运行命令
phpartisan db:seed
时,什么也没有发生

我找不到原因。我对拉威尔是个新手

数据库名为“Laravel”,表名为“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
方法可以将数据库种子划分为多个文件,这样就不会有一个种子类变得过大。传递要运行的播种机类的名称:

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

您还可以通过命令
php artisan db:seed--class=yourseedername

同样,在播种机代码中,您将两次插入相同的数据,一次是通过DB查询,另一次是从书本模型。因此,您将重复相同的数据两次。我认为应该以列冲突的形式抛出一个错误,说明完整性约束冲突,因为图书的isbn是唯一的。

如果这没有帮助,请运行:composer dump autolaid如果没有错误,那么您希望在迁移文件中为isnb列添加unique()约束。