Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Neeraj1005's avatar

>>> use App\{User,BookingEvent,BookingService} >>> BookingEvent::factory()->count(3)->for(BookingService::factory())->create(); PHP Error: Class 'Database/Factories/BookingEventFactory' not found in C:/laragon/www/acl/vendor/laravel/framework/src/Illumin

In my laravel project having version 8.9.0. I'm trying to run factory command but this show this kind of error in tinker terminal

λ php artisan tinker
Psy Shell v0.10.4 (PHP 7.4.8 — cli) by Justin Hileman
>>> use App\{User,BookingEvent,BookingService}
>>> BookingEvent::factory()->count(3)->for(BookingService::factory())->create();
PHP Error:  Class 'Database/Factories/BookingEventFactory' not found in C:/laragon/www/acl/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php on line 656

can anyone tells me what I did wrong. THis is my factory class method BookingEventFactory

<?php

namespace Database\Factories;

use App\BookingEvent;
use App\BookingService;
use Illuminate\Database\Eloquent\Factories\Factory;

class BookingEventFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = BookingEvent::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        return [
            'event_name' => $this->faker->name,
            'user_id' => 2,
            'booking_service_id' => BookingService::factory(),
            'duration' => '1 Hours',
            'price' => '1000',
            'event_description' => $this->faker->paragraph,
        ];
    }
}

0 likes
13 replies
divspace's avatar

Have you tried composer dump-autoload?

1 like
Sinnbeck's avatar

Also make sure it is inside a directory database\Factories

Neeraj1005's avatar

@divspace

Have you tried composer dump-autoload?

No.

@sinnbeck

Also make sure it is inside a directory database\Factories

It is inside database\Factories...

@divspace @sinnbeck basically I upgraded my project laravel7.x to laravel8.x. And in my project all models are inside of app not in models\model.php. And I installed the factory legacy pkg...

Sinnbeck's avatar

And I installed the factory legacy pkg...

Why? You are using the new laravel 8 factory syntax?

Neeraj1005's avatar

@sinnbeck

Why? You are using the new laravel 8 factory syntax?

THis project is upgraded from laravel 7 to laravel 8 and in previous I hae used the laravel 7 syntax.

SHould I have to remove this legacy pkg?

Sinnbeck's avatar

Try removing the file and recreating it using the terminal

php artisan make:factory BookingEventFactory --model BookingEvent
Sinnbeck's avatar

Well you dont need it. And sorry made a typo before. the directory should be lowercase f database\factories

Also make sure you have this in your composer.json file

"autoload": {
        "psr-4": {
            "App\": "app/",
            "Database\Factories\": "database/factories/",
            "Database\Seeders\": "database/seeders/"
        }
    },
Neeraj1005's avatar

@sinnbeck

    "autoload": {
        "psr-4": {
            "App\": "app/"
        },
        "classmap": [
            "database/seeds",
            "database/factories"
        ]
    },

I have this in my composer.json. SHould i have change this?

Sinnbeck's avatar

Oh and remember composer dump-autoload afterwards!

Neeraj1005's avatar

@sinnbeck After removing pkg and update the composer Now this kind of error occurs.

λ php artisan db:seed

   Illuminate\Contracts\Container\BindingResolutionException

  Target class [UserSeeder] does not exist.

IN my directory I have all seeder

database/seeders/UserSeeder

<?php

use App\User;
use Illuminate\Support\Str;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $user = User::factory()->times(3)->create();
    }
}
Sinnbeck's avatar

It is missing the namespace at the top :)

namespace Database\Seeders;
1 like

Please or to participate in this conversation.