1

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,
        ]);
      }
  }
}
Mahdi Younesi
  • 6,889
  • 2
  • 20
  • 51
AltBrian
  • 2,392
  • 9
  • 29
  • 58

3 Answers3

4

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'];
 }
Mahdi Younesi
  • 6,889
  • 2
  • 20
  • 51
2

I think Directory is a model here.

<?php

use Illuminate\Database\Seeder;
use Faker\Factory as Faker;
use App\Directory;
....
sumit sharma
  • 698
  • 5
  • 16
0

I have a similar experience. Make sure, the Php Curl is installed and enabled before performing any seed operation. You can install and enable by typing following command.

sudo apt install php-curl
Ahmad Sharif
  • 4,141
  • 5
  • 37
  • 49