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:
@@ -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`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user