Laravel db:种子成功但我的数据库中没有数据

Rah*_*ama 0 php laravel eloquent

我试图在 Laravel 中制作简单的种子,但是当我执行“php artisan db:seed”时,它没有填满我的领域。

我制作了我的类别播种机文件,这是我的类别播种机文件:

class CategoryTableSeeder extends Seeder
{
    public function run()
    {
        $categori1 = new Category;
        $categori1->name = "Books";
        $categori1->save();
        $categori2 = new Category;
        $categori2->name = "Backpacks";
        $categori2->save();

    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的类别模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    //
}
Run Code Online (Sandbox Code Playgroud)

这是我的类别表迁移文件的一部分:

    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }
Run Code Online (Sandbox Code Playgroud)

小智 5

您必须将 Seeder 类添加到DatabaseSeeder.php. 像这样:

public function run()
{
    $this->call(CategoryTableSeeder::class);
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以指定要使用种子的确切类php artisan db:seed --class=CategoryTableSeeder

如果这有帮助,请告诉我。