简体   繁体   English

Laravel 5.3调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Factory :: state()

[英]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.

相关问题 调用未定义的方法Illuminate \\ Database \\ Query \\ Builder :: attach()在laravel 5.3中 - Call to undefined method Illuminate\Database\Query\Builder::attach() In laravel 5.3 Laravel 5.3,调用未定义的方法Illuminate \\ Database \\ Query \\ Builder :: links() - Laravel 5.3, Call to undefined method Illuminate\Database\Query\Builder::links() 调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: save()laravel - Call to undefined method Illuminate\Database\Eloquent\Collection::save() laravel 调用未定义的方法 Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::type() [Laravel] - Call to undefined method Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::type() [Laravel] Laravel:调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: where() - Laravel: Call to undefined method Illuminate\Database\Eloquent\Collection::where() Laravel 分页 - 调用未定义的方法 Illuminate\\Database\\Eloquent\\Builder::links() - Laravel Pagination - Call to undefined method Illuminate\Database\Eloquent\Builder::links() Laravel 5 - 调用未定义的方法 Illuminate\Database\Eloquent\Collection::Paginate() - Laravel 5 - Call to undefined method Illuminate\Database\Eloquent\Collection::Paginate() Laravel 7.6 调用未定义的方法 Illuminate\Database\Eloquent\Builder::appends() - Laravel 7.6 Call to undefined method Illuminate\Database\Eloquent\Builder::appends() LARAVEL 调用未定义的方法 Illuminate\\Database\\Eloquent\\Builder::splice() - LARAVEL Call to undefined method Illuminate\Database\Eloquent\Builder::splice() Laravel 5:调用未定义的方法Illuminate \\ Database \\ Eloquent \\ Collection :: exists() - Laravel 5: Call to undefined method Illuminate\Database\Eloquent\Collection::exists()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM