首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >龙舌兰8号扩展模型工厂

龙舌兰8号扩展模型工厂
EN

Stack Overflow用户
提问于 2020-10-12 15:45:12
回答 1查看 374关注 0票数 3

我正面临一个关于拉拉8号的问题,我创造了:

扩展Illuminate\Database\Eloquent\Model

  • A模型的名为CRUDModel的类,扩展CRUDModel

  • A工厂的Stuff,称为StuffFactory

当我调用Stuff::factory()->count(60)->create();时,会得到以下错误:

代码语言:javascript
复制
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,但我不确定。这是我的课:

代码语言:javascript
复制
<?php
namespace App\Crudite\Model;

use Illuminate\Database\Eloquent\Model;
use App\Crudite\Model\CRUDIntel;

class CRUDModel extends Model
{
    use CRUDIntel;

    ...
    
}
代码语言:javascript
复制
<?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;
    ...
}
代码语言:javascript
复制
<?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"
        ];
    }
}
EN

回答 1

Stack Overflow用户

发布于 2021-12-10 07:06:49

对于其他可能有同样问题的人来说,@Kermito用扩展模型制造工厂的做法实际上是正确的。只需使用扩展模型创建一个工厂,就像使用常规模型一样,它应该可以工作。但是,在这种情况下,有错误的是titlecontenttitle函数,这可能是为什么它们没有像错误中所示的那样添加到insert语句中的原因。Faker已经更新了它们的语法,代码行应该是:

代码语言:javascript
复制
//note the parantheses ()
 "title" => $this->faker->name(),
 "content" => $this->faker->paragraph(),
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64321035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档