Interview frequency: …
Short answer
They let one model (such as Comment) relate to multiple model types through commentable_id and commentable_type; storing full class names is fragile and should be replaced with a morphMap.
Explanation
With polymorphic relationships, a comment can belong to a Post or a Video. The table stores the object type and its ID. If it stores the full class name, such as App\Models\Post, renaming the class or its directory breaks existing relationships. A robust solution is to register a stable mapping with Relation::morphMap(['post' => Post::class]) in AppServiceProvider.
Code example
Relation::morphMap([
'post' => \App\Models\Post::class,
'video' => \App\Models\Video::class,
]);Common mistakes
- Letting Eloquent store the full namespace path in the database without using morphMap.