laravel 8中扩展模型的工厂

时间:2020-10-12 15:45:12

标签: php laravel factory extends 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"
        ];
    }
}

0 个答案:

没有答案