Php 在laravel中找不到工厂类
请协助,我不明白为什么在无数次检查我的代码后找不到错误类“Database\Factorys\BookingFactory”Php 在laravel中找不到工厂类,php,laravel,laravel-seeding,Php,Laravel,Laravel Seeding,请协助,我不明白为什么在无数次检查我的代码后找不到错误类“Database\Factorys\BookingFactory” class ModelFactory extends Factory MyBookingFactory.php <?php namespace Database\Factories; use App\Models\Booking; use Illuminate\Database\Eloquent\Factories\Factory; use Carbon\Ca
class ModelFactory extends Factory
MyBookingFactory.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,
];
}
}
您的类名应该与文件名BookingFactory
匹配,因为您将类命名为ModelFactory
而不是BookingFactory
…哦,我的上帝。。。我花了一整天的时间试图找出问题所在。谢谢@Thomas,找不到类的错误不再存在!如果可以的话,在修好后还有一个SQLSTATE[HY000]:一般错误:1364字段“bookable\u id”没有默认值(SQL:insert-into'bookings`(`from`、`to`、`updated\u at`、`created\u at`)值(2020-09-23 11:12:282020-09-28 11:12:282020-09-21 15:23:442020-09-21 15:23:44))
我已经修复了,谢谢你救了我一天!
<?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);
});
}
}
class ModelFactory extends Factory