Thursday, March 30, 2017

Laravel. Model Factory. Many-To-Many relation

I have problem with many-to-many relation on model factory.

For example:
There are users, roles and user_role tables. I have defined model factories for user and roles. Next I try to add role to user:

$role = factory(Role::class)->create();
config(['admin_group_id' => $role->id]);
$admin = factory(User::class)->create();
$admin->roles()->save($role);

It's work, but when I get $admin->roles()->first()->id it not equal $role->id



via David Goloviy

Advertisement