UNPKG

kira-crud

Version:

Intelligent CRUD Generator for Laravel and Angular

92 lines (68 loc) 2.73 kB
# Polymorphic Relationship Templates This directory contains templates for generating code that supports polymorphic relationships in Laravel and Angular. ## What are Polymorphic Relationships? Polymorphic relationships allow a model to belong to more than one other model on a single association. For example, you might have a `Comment` model that belongs to either a `Post` model or a `Video` model. ## Types of Polymorphic Relationships ### 1. Morphs To (Polymorphic One-to-One or One-to-Many) In this relationship, a model can belong to more than one other model type. For example: - A Comment can belong to a Post or a Video - An Image can belong to a User or a Product ### 2. Morph Many (Inverse of Morphs To) The opposite side of a "morphs to" relationship. For example: - A Post can have many Comments (polymorphic) - A Product can have many Images (polymorphic) ### 3. Many-to-Many Polymorphic Relations Allows complex relationships between models through a pivot table. For example: - A Tag that can be assigned to either Posts or Videos - A Product that can be related to multiple Users with different roles ## How to Use These Templates The templates in this directory are used by the CRUD generator to create: 1. **Database Migrations** with proper polymorphic columns 2. **Laravel Models** with appropriate relationship methods 3. **Angular Components** that can handle polymorphic data display and selection 4. **Form Components** for creating and editing polymorphic relationships ## Configuration Example Here's an example configuration for a polymorphic relationship: ```yaml model: name: Comment tableName: comments fields: - name: body type: text relationships: - name: commentable type: morphTo polymorphicTypes: - model: Post displayField: title - model: Video displayField: name ``` For the inverse side (morph many): ```yaml model: name: Post tableName: posts fields: - name: title type: string relationships: - name: comments type: morphMany model: Comment morphName: commentable ``` ## Database Structure For a polymorphic relationship like `commentable` on the `Comment` model, the migration will create: ```php // comments table migration $table->text('body'); $table->morphs('commentable'); // Creates commentable_id and commentable_type columns ``` ## Limitations and Considerations When using polymorphic relationships: 1. Foreign key constraints cannot be applied directly in the database 2. Extra validation is needed to ensure data integrity 3. UI implementation requires dynamic component loading 4. API endpoints need special handling for polymorphic data