Search code examples
phplaravellaravel-5fakerlaravel-seeding

Call to undefined method Directory::create() when seeding in Laravel


I am trying to seed a database in Laravel. I am using faker to seed the database but I am getting the following error.

Call to undefined method Directory::create()

Below is the code I have written in the table seed file. Basically, I want to create a seed of names and telephone numbers. Below is the code I have written.

<?php

 use Illuminate\Database\Seeder;
 use Faker\Factory as Faker;

class DirectoriesTableSeeder extends Seeder
{
   /**
   * Run the database seeds.
  *
   * @return void
  */
 public function run()
  {
       //Directory::truncate();
       $faker = \Faker\Factory::create();
      for ($i = 0; $i < 50; $i++) {
        Directory::create([
            'name' => $faker->name,
            'number' => $faker->PhoneNumber,
        ]);
      }
  }
}

Solution

  • You need to use the model in top of file

    <?php
    
    use Illuminate\Database\Seeder;
    use Faker\Factory as Faker;
    use App\Directory;
    

    Or call the model with its namespace

    App\Directory::create([
            'name' => $faker->name,
            'number' => $faker->PhoneNumber,
    ]);
    

    And edit your $fillable propetry's definition in model

    It should be protected instead of protect

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