[英]Laravel 5.3 Call to undefined method Illuminate\Database\Eloquent\Factory::state()
I'm trying to define a few different variations of a User model for testing, using Laravels ModelFactory as documented here 我试图定义一个用户模式测试的几个不同的版本,使用Laravels ModelFactory作为记录在这里
$factory->define(App\User::class, function(\Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'remember_token' => str_random(10),
'phone' => $faker->phoneNumber,
];
});
$factory->state(App\User::class, 'admin', function (Faker\Generator $faker) {
return [
'groups' => function(App\User $u) {
return App\Models\Group::where('level', '<=', 5)->get()->toArray();
}
];
});
And then I create a User model: 然后我创建一个User模型:
$user = factory(User::class)->states('admin')->make();
But phpunit seems to exit out of the test without complaining. 但phpunit似乎没有抱怨就退出了测试。 In the PHP logs, I see:
在PHP日志中,我看到:
Call to undefined method Illuminate\Database\Eloquent\Factory::state()
There isn't very much documentation on the state() method in Laravel docs, and I've searched and experimented for hours with no progress to show for it. Laravel文档中没有关于state()方法的文档,我已经搜索和实验了几个小时而没有任何进展来展示它。
As a sidenote: the groups
attribute is referring to a Many relationship. 作为旁注:
groups
属性指的是Many关系。 However, this Exception is thrown regardless of which model I am creating, even simple models. 但是,无论我创建哪种模型,甚至是简单模型,都会抛出此异常。
After digging around in the Illuminate\\Database\\Eloquent\\Factory and FactoryBuilder classes, I discovered that the state() and states() methods were both missing, compared to the latest Laravel branch on github. 在Illuminate \\ Database \\ Eloquent \\ Factory和FactoryBuilder类中挖掘之后,我发现与github上的最新Laravel分支相比,state()和states()方法都丢失了。 After running composer update, it updated me to Laravel Framework v5.3.18, and now the ModelFactory states work as expected.
在运行composer update之后,它将我更新为Laravel Framework v5.3.18,现在ModelFactory状态按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.