我正面临一个关于拉拉8号的问题,我创造了:
扩展Illuminate\Database\Eloquent\Model
CRUDModel的类,扩展CRUDModel
Stuff,称为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"
];
}
}发布于 2021-12-10 07:06:49
对于其他可能有同样问题的人来说,@Kermito用扩展模型制造工厂的做法实际上是正确的。只需使用扩展模型创建一个工厂,就像使用常规模型一样,它应该可以工作。但是,在这种情况下,有错误的是title和content的title函数,这可能是为什么它们没有像错误中所示的那样添加到insert语句中的原因。Faker已经更新了它们的语法,代码行应该是:
//note the parantheses ()
"title" => $this->faker->name(),
"content" => $this->faker->paragraph(),https://stackoverflow.com/questions/64321035
复制相似问题