Becoming
Recently I wrote a method to allow us to convert a model into something another one. Something like:
```php
class Room extends Model
{
use HasChildren;
protected $childTypes = [
'closed' => Closed::class,
'open' => Open::class,
];
}
class Closed extends Room
{
use HasParent;
}
class Open extends Room
{
use HasParent;
}
$room = Closed::findOrFail(1); // Instance of Closed
$room = $room->become(Open::class); // Instance of Open is returned (the type would be set to "open")
```
I have a version of that [here](https://github.com/tonysm/turbo-chat-demo/blob/main/app/Models/Room.php#L57-L68), but need polish that.
关闭于 2025-12-05 1 条评论