Laravel 5.3 Method call undefined Illuminate \ Database \ Eloquent \ Factory :: state ()

I am trying to define several different variations of a user model for testing using the Laravels ModelFactory, as described here

$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 = factory(User::class)->states('admin')->make(); 

But phpunit seems to exit the test without complaining. In PHP logs, I see:

 Call to undefined method Illuminate\Database\Eloquent\Factory::state() 

There is not much documentation for state () in Laravel docs, and I searched and experimented for several hours without any success to show this.

As a side element: the groups attribute refers to the Many relationship. However, this exception is thrown no matter what model I create, even simple models.

+5
source share
1 answer

After delving into the Light \ Database \ Eloquent \ Factory and FactoryBuilder classes, I found that the state () and state () of the method are missing, compared to the last Laravel branch on GitHub. After completing the upgrade for the linker, he upgraded me to Laravel Framework v5.3.18, and now ModelFactory states are working properly.

+2
source

Source: https://habr.com/ru/post/1258348/


All Articles