简体   繁体   English

如何在Laravel的另一个工厂中创建User实例?

[英]How do you create a User instance in another factory in Laravel?

I am trying to create some test data using factories in Laravel 5.5. 我正在尝试使用Laravel 5.5中的工厂创建一些测试数据。 I am running this 我正在运行

factory('App\Thread', 3)->create();

I get the error message Column 'user_id' cannot be null 我收到错误消息列“ user_id”不能为空

Column 'user_id' cannot be null (SQL: insert into thread
s` (user_id, title, body, updated_at, created_at) values (, Quia modi
aperiam eos aut., Sed voluptate ut quia ut. Et accusamus eaque est amet. Veniam
consectetur exercitationem id facere., 2017-09-04 04:38:11, 2017-09-04 04:38:11)
)'

My Thread factory creates a new instance user, but it doesn't seem to return a value. 我的线程工厂创建了一个新的实例用户,但似乎没有返回值。

use Faker\Generator as Faker;

$factory->define(App\Thread::class, function (Faker $faker) {

return [
    'user_id' => function () {
                return factory('App\User')->make()->id;
    },
    'title' => $faker->sentence,
    'body' => $faker->paragraph,
];
});

I haven't modified the user factory at all. 我根本没有修改用户工厂。 I have copied and pasted from the documentation and cannot get it. 我已经从文档中复制并粘贴了文件 ,但无法获取。 Any help would be appreciated I'm new to this and I'm sure it's something simple. 任何帮助将不胜感激,我是新手,我敢肯定这很简单。

I was typing 我在打字

factory(App\Thread::class, 3)->create();

instead of 代替

factory('App\Thread', 3)->create();

I did not know they were different. 我不知道他们是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM