0
votes

I tried to make simple seed in laravel, but when I did "php artisan db:seed", it didn't fill my field.

I made my Categories seeder file this is my Categories seeder file :

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

    }
}

This is my Category model :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    //
}

And this is part of my Categories table migration file :

    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->timestamps();
        });
    }
3
did you already call the seeder class on database seeder? - Ghiffari Assamar

3 Answers

6
votes

You have to add your Seeder class to DatabaseSeeder.php. Like so:

public function run()
{
    $this->call(CategoryTableSeeder::class);
}

Or you can specify the exact class that you want to seed with php artisan db:seed --class=CategoryTableSeeder.

4
votes

Run

php artisan db:seed --class=CategoryTableSeeder

else if you want to run all seeder add this class in file DatabaseSeeder.php

$this->call(CategoryTableSeeder::class);
-1
votes

The reason why this happened is because you forgot to add $fillable to your model, and define which fields are fillabe. Inside your Category add this:

class Category extends Model
{
    protected $fillable = ['name'];
}

Run your seeder again, you should have desired data.