Laravel - Database - Many To Many Relationships Using Magic Methods
For convenience, you may use Laravel's magic factory relationship methods to define many to many relationships. For example, the following example will use convention to determine that the related models should be created via a roles
relationship method on the User
model:
$user = User::factory()
->hasRoles(1, [
'name' => 'Editor'
])
->create();