【问题标题】:Why is there a default id field in factory Laravel?为什么工厂 Laravel 中有一个默认的 id 字段?
【发布时间】:2019-07-07 15:16:44
【问题描述】:

我为我的问题模型创建了一个问题工厂。我在问题模型中没有任何 id 字段,但我不明白工厂在哪里创建一个。

工厂

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\Question;use Faker\Generator as Faker;

$factory->define(Question::class, function (Faker $faker) {
    return [
        'qid' => $faker->name,
        'question' => $faker->sentence
    ];
});

廷克工厂的产出。

Psy Shell v0.9.9 (PHP 7.2.19 — cli) by Justin Hileman
>>> factory('App\Question')->create();
=> App\Question {#3001
     qid: "Bell Schamberger",
     question: "Consequatur debitis est nemo.",
     updated_at: "2019-07-07 15:08:27",
     created_at: "2019-07-07 15:08:27",
     id: 0,
   }

【问题讨论】:

  • 迁移也没有什么?

标签: php laravel factory


【解决方案1】:

Eloquent 假设每个表都有一个名为 'id' 的主键

你可以像这样覆盖它(例如在你的模型中):

protected $primaryKey = 'youre_pk_field';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-29
    • 1970-01-01
    • 2018-02-14
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    相关资源
    最近更新 更多