我在laravel 8上遇到了一个问题:
CRUDModel
的类,它扩展了Illuminate\Database\Eloquent\Model
Stuff
的模型,扩展了CRUDModel
StuffFactory
的工厂致电Stuff::factory()->count(60)->create();
时出现此错误:
SQLSTATE[HY000]: General error: 1364 Field 'title' doesn't have a default value (SQL: insert into `stuffs` (`updated_at`, `created_at`) values (2020-10-12 15:28:06, 2020-10-12 15:28:06))
我想是因为Stuff
没有直接扩展Model
,但是我不确定。
这是我的课:
<?php
namespace App\Crudite\Model;
use Illuminate\Database\Eloquent\Model;
use App\Crudite\Model\CRUDIntel;
class CRUDModel extends Model
{
use CRUDIntel;
...
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Crudite\Model\CRUDModel;
class Stuff extends CRUDModel
{
use HasFactory;
...
}
<?php
namespace Database\Factories;
use App\Models\Stuff;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Factories\Factory;
class StuffFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Stuff::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
"title" => $this->faker->name,
"content" => $this->faker->paragraph,
"thumbnail" => "https://placekitten.com/1200/300"
];
}
}