laravel 7x无法播种 [英] laravel 7x can't seeding

查看:27
本文介绍了laravel 7x无法播种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用laravel中的模型作为种子.

I'm trying to seed with models in laravel.

我正在使用xampp创建数据库并检查数据.表正在创建,但是播种不起作用.(似乎正在批量处理)

I'm using xampp to create database and check data. Table is creating but seeding is not working. (seems working on batch)

这里是代码,请检查.谢谢:)

Here is code, please check. Thanks :)

迁移表(2020_08_08_092121_create_books_table.php)

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    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();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('books');
    }
}

模型表(Book.php)


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    
}

种子表(BookSeeder.php)


<?php

use Illuminate\Database\Seeder;

class BookSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $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);
        }
    }
}

推荐答案

您需要先使模型上的字段可填充,然后才能使用 :: create()方法.

You need to make the fields on the model fillable before you can use the ::create() method like that.

在这里查看: https://laravel.com/docs/7.x/eloquent#mass-assignment

示例:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    protected $fillable = [
        'name',
        'writer_name',
        'isbn'
    ];
}

一点说明.如果您的表名不是模型 Book/books 的复数形式,那么您还需要在模型上指定一个表名.但是在您的情况下,Laravel希望将该表命名为"books",所以您很好.即使我会提到这一点,以防万一您不知道.

A little side note. If your table name was not the pluralised version of your model Book / books, then you would also need to specify a table name on the model. But in your case Laravel expects the table to be named "books", so you are good. Just though I would mention this, in case you don't already know.

这篇关于laravel 7x无法播种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆