tighten / parental

Use single table inheritance in your Laravel app
MIT License
1.36k stars 98 forks source link

Allow use of Enum cast on inheritance column #93

Closed inmanturbo closed 3 years ago

inmanturbo commented 3 years ago

This change allows the type column to be cast to an enum, and for that enum to be mapped to a child type, I.e:

class User 
{
use \Parental\HasChildren;

    protected $childTypes = [
        'admin' => Admin::class,
        'user' => User::class,
    ];

    protected $casts = [
        'type' => UserTypeEnum::class,
    ];
}
...
/**
 * @method static self admin()
 * @method static self user()
 */
class UserTypeEnum extends Enum
{
}

Or in php 8.1:

enum UserTypeEnum
{
    case USER = 'user';
    case ADMIN = 'admin';
}