-1

Kindly assist, I don't understand why I'm getting the error Class 'Database\Factories\BookingFactory' not found after going through my code countless times!

My BookingFactory.php

<?php

namespace Database\Factories;

use App\Models\Booking;
use Illuminate\Database\Eloquent\Factories\Factory;
use Carbon\Carbon;
use Illuminate\Support\Str;

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

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {

        $from = Carbon::instance($this->faker->dateTimeBetween('-1 months', '+1 months'));
        $to = (clone $from)->addDays(random_int(0, 14));
        return [
            'from' => $from,
            'to' => $to,
            
        ];
    }
}

This is My Table Seeder

<?php

namespace Database\Seeders;
use App\Models\Booking;
use App\Models\Bookable;
use Illuminate\Database\Seeder;

class BookingsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Bookable::all()->each(function (Bookable $bookable) {
            $booking = Booking::factory()->times(1)->create();
                      
            
            $bookings = collect([$booking]);

            for ($i = 0; $i < random_int(1, 20); $i++) {
                $from = (clone $booking->to)->addDays(random_int(1, 14));
                $to = (clone $from)->addDays(random_int(0, 14));

                $booking = Booking::make([
                    'from' => $from,
                    'to' => $to,
                    

                    
                ]);
                $bookings->push($booking);
            }

            $bookable->bookings()->saveMany($bookings);
        });
    }
}

This is my Database Seeder

<?php

namespace Database\Seeders;
use App\Models\Booking;
use App\Models\Bookable;
use Illuminate\Database\Seeder;

class BookingsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Bookable::all()->each(function (Bookable $bookable) {
            $booking = Booking::factory()->times(1)->create();
                      
            
            $bookings = collect([$booking]);

            for ($i = 0; $i < random_int(1, 20); $i++) {
                $from = (clone $booking->to)->addDays(random_int(1, 14));
                $to = (clone $from)->addDays(random_int(0, 14));

                $booking = Booking::make([
                    'from' => $from,
                    'to' => $to,
                    

                    
                ]);
                $bookings->push($booking);
            }

            $bookable->bookings()->saveMany($bookings);
        });
    }
}

Kindly assist, I don't understand why I'm getting the error Class 'Database\Factories\BookingFactory' not found after going through my code countless times!

Alphy Gacheru
  • 489
  • 1
  • 9
  • 28

1 Answers1

1
class ModelFactory extends Factory

Your class name should match the file name BookingFactory

Zoe Edwards
  • 12,999
  • 3
  • 24
  • 43
  • Oh my God... I've spent the whole day trying to find the problem. Thanks @Thomas the error of class not found is no more! Got another after fixing that help out if you can. ```SQLSTATE[HY000]: General error: 1364 Field 'bookable_id' doesn't have a default value (SQL: insert into `bookings` (`from`, `to`, `updated_at`, `created_at`) values (2020-09-23 11:12:28, 2020-09-28 11:12:28, 2020-09-21 15:23:44, 2020-09-21 15:23:44))``` – Alphy Gacheru Sep 21 '20 at 15:28
  • I've fixed it, thanks you've saved my day! – Alphy Gacheru Sep 21 '20 at 15:46