pindab0ter/constrained-morph-to-for-laravel
Comment model that can only belong to Post or Video models, but not User or Tag). This reduces runtime errors and improves data consistency.Image or Video but not UserProfile).Notification can only target User, Team, or Project models, not arbitrary classes.Review models to only relate to Product or Bundle types.Adopt when:
morphTo) but requires type constraints to prevent invalid associations (e.g., a Comment tied to a User model by accident).Look elsewhere if:
morphTo) in your app.accessors or mutators) over compile-time constraints.For Executives:
"This package lets us enforce ‘type safety’ for polymorphic relationships in Laravel—meaning a Comment can only link to Post or Video models, not arbitrary classes. This reduces bugs, improves data integrity, and aligns with our goal of [scalability/performance/security]. It’s a lightweight, MIT-licensed solution that saves dev time vs. building custom validation. Ideal for [CMS/SaaS/e-commerce] projects where polymorphic flexibility meets strict data rules."
For Engineers:
"This extends Laravel’s morphTo to reject invalid model types at the query level. For example:
// Only allows Post or Video models
public function commentable()
{
return $this->morphTo()->constrained(['App\Models\Post', 'App\Models\Video']);
}
Benefits:
How can I help you explore Laravel packages today?