Home>

I want to prepare test data at Cedar and Factory.
https://stackoverflow.com/questions/63816395/laravel-call-to-undefined-function-database-seeders-factory
I wrote the code referring to this article. But that doesn't work.

Laravel is using version 8.
I'm wondering if there is a cause in BlogTableSeeder.php, but I don't know the cause. I hope you'll tell me.
Thank you( ;;)

Seeding: Database \ Seeders \ BlogsTableSeeder
   BadMethodCallException
  Call to undefined method App \ Models \ Blog :: factory ()
  at vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php: 50
     46 ▕ * @throws \ BadMethodCallException
     47 ▕ * /
     48 ▕ protected static function throwBadMethodCallException ($method)
     49 ▕ {
  ➜ 50 ▕ throw new BadMethodCallException (sprintf (sprintf (sprintf)
     51 ▕'Call to undefined method% s ::% s ()', static :: class, $method
     52 ▕));
     53▕}
     54 ▕}
  • Bad Method Call: Did you mean App \ Models \ Blog :: toArray ()?
      +3 vendor frames
  4 database/seeders/BlogsTableSeeder.php: 17
      Illuminate \ Database \ Eloquent \ Model :: __callStatic ("factory", [])
      +7 vendor frames
  12 database/seeders/DatabaseSeeder.php: 17
      Illuminate \ Database \ Seeder :: call ("Database \ Seeders \ BlogsTableSeeder")
Corresponding source code

BlogsTableSeeder.php

<? php
namespace Database \ Seeders;
use App \ Models \ Blog;
use Illuminate \ Database \ Seeder;
class BlogsTableSeeder extends Seeder
{
   /**
     * Run the database seeds.
     *
     * @return void
     * /
    public function run ()
    {
        Blog :: factory ()->count (15)->create ();
    }
}


DatabaseSeeder.php

<? php
namespace Database \ Seeders;
use Illuminate \ Database \ Seeder;
class DatabaseSeeder extends Seeder
{
   /**
     * Seed the application's database.
     *
     * @return void
     * /
    public function run ()
    {
        $this->call
        (BlogsTableSeeder :: class);
    }
}


BlogFactory.php

<? php
namespace Database \ Factories;
use App \ Models \ Blog;
use Illuminate \ Database \ Eloquent \ Factories \ Factory;
use Illuminate \ Support \ Str;
class BlogFactory extends Factory
{
   /**
     * The name of the factory's corresponding model.
     *
     * @var string
     * /
    protected $model = Blog :: class;
   /**
     * Define the model's default state.
     *
     * @return array
     * /
    public function definition ()
    {
        return [
            'title' =>$this->faker->word,
            'content' =>$this->faker->realText
        ];;
    }
}
Supplementary information (FW/tool version, etc.)

The version of Laravel is 8.14.0.

  • Answer # 1

    App \ Models \ BlogNeed code.
    Probably not enoughHasFactory
    It should be written in User.php, so you can copy it.