Add get_polymorphic_parent() helper for type_ref_columns compatibility

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
root
2026-01-07 05:24:15 +00:00
parent 993be621a2
commit 36979e4b52
2 changed files with 48 additions and 5 deletions

View File

@@ -828,18 +828,27 @@ Pattern recognition:
## POLYMORPHIC TYPE REFERENCES
Polymorphic `*_type` columns (fileable_type, taskable_type, etc.) store BIGINT integers in the database for efficiency. The framework maps between integer IDs and class names transparently.
Polymorphic `*_type` columns store BIGINT integers. Framework maps to class names transparently.
```php
// Model: Declare type ref columns
protected static $type_ref_columns = ['fileable_type'];
// Usage: Code works with class name strings - conversion is automatic
$attachment->fileable_type = 'Contact_Model'; // Stored as integer in DB
$attachment->fileable_type = 'Contact_Model'; // Stored as integer
echo $attachment->fileable_type; // Reads as 'Contact_Model'
```
**Simple Names Only**: Always use `class_basename($model)`, never `get_class($model)` (FQCNs)
**NEVER USE morphTo()**: Laravel's `morphTo()` bypasses accessors, receives integers, crashes. Use `get_polymorphic_parent()`:
```php
// ❌ WRONG
public function parent() { return $this->morphTo(...); }
// ✅ CORRECT
public function getParentAttribute() {
return $this->get_polymorphic_parent('parent_type', 'parent_id');
}
```
**Simple Names Only**: Use `class_basename($model)`, never `get_class($model)`
Details: `php artisan rsx:man polymorphic`