A
A
Andrei2582022-01-13 00:40:47
Laravel
Andrei258, 2022-01-13 00:40:47

How to fix "Class not found" error?

I have laravel code.
testseeder.php

<?php

namespace Database\Seeders;
use App\Models\Myuser;
use Illuminate\Database\Seeder;

class TestSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Myuser::factory()->count(100)->create();
    }
}


TestFactory.php
<?php

namespace Database\Factories;
use App\Models\Myuser;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class TestFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
          'lastname' => $this->faker->name(),
          'firstname' => $this->faker->firstNameMale(),
          'email' => $this->faker->email(),
          'phone' => $this->faker->e164PhoneNumber(),
          'balance' => $this->faker->name(),
          'package' => $this->faker->name(),
          'notification' => $this->faker->name(),
          'img' => $this->faker->colorName(),
          'password' => $this->faker->sha256(),
          'ip' => $this->faker->sha1(),
          'created_at' => $this->faker->dateTime(),
          'updated_at' => $this->faker->dateTime(),
        ];
    }
}


When executing the code through the command line I get the error I don't know how to fix it.
php artisan db:seed --class=TestSeeder
Class 'Database\Factories\MyuserFactory' not found

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
FernandoErrNando, 2022-01-13
@FernandoErrNando

Try to be a little more careful, I'll give you a hint: you are trying to call a class factory (MyuserFactory) in the seeder, which you don't have, but have TestFactory. So we need to change...

V
vism, 2022-01-13
@vism

create class Database\Factories\MyuserFactory

T
Timur Maslov, 2022-01-13
@tmaslov22

Rename TestFactory to MyuserFactory. Your model name is Myuser and not Test. See how the factory() method works. It takes the name of the model + 'Factory' = the name of the class that is used next.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question