I have User
and Activity
models. User
hasMany relationship to Activity
(and an Activity
belongsTo only one User
):
In User model:
public function activities() {
return $this->hasMany(Activity::class);
}
I want to seed the users with their corresponding activities. I tried this but did not work:
public function run()
{
factory(App\User::class, 5)->create()->each(function ($user) {
$user->activities()->saveMany(factory(App\Activity::class, 2)->make());
});
}
ActivityFactory:
$factory->define(App\Activity::class, function (Faker $faker) {
return [
'title' => $faker->text(50),
'description' => $faker->text(200)
];
});
What am I doing wrong?
Activity
model. – Rwd