Laravel - Database - Morph To Relationships
Magic methods may not be used to create morphTo
relationships. Instead, the for
method must be used directly and the name of the relationship must be explicitly provided. For example, imagine that the Comment
model has a commentable
method that defines a morphTo
relationship. In this situation, we may create three comments that belong to a single post by using the for
method directly:
$comments = Comment::factory()->count(3)->for(
Post::factory(), 'commentable'
)->create();