如何解决laravel 8中扩展模型的工厂
我在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"
];
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。