Cinek's avatar
Level 6

Model events in database seeder

Hi, I have one confusion. I have 2 models: User and Group. Group have user_id which means owner of this group. I fill this ID in GroupObserver in creating function

$group->user()->associate(Auth::user());

I have problem because it will fail:

        factory(\App\Models\Group::class, 10)->create();

while seeding there is not logged user. I assigned user to group in GroupFactory and disable all events on Group model but I think there's a better way to workarround this problem.

0 likes
2 replies
Cinek's avatar
Level 6

user_id column is set to not null so when model events are enabled there's an error on user_id constraint - Auth::user() is null so can't attach null user to group.

Please or to participate in this conversation.