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();
});
}
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
.
Let me know if this helped.
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);
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.