Closed kayrunm closed 2 years ago
+1 for this issue with using php 8.1 enums as the $childTypes identifiers.
PHP 8.0 using class constants was fine.
class ProductType
{
public const LOAN = 1;
public const CREDIT_CARD = 2;
}
class ProductResult extends Model
{
use HasChildren;
protected $childColumn = 'product_type';
protected $childTypes = [
ProductType::LOAN => LoanResult::class,
ProductType::CREDIT_CARD => CreditCardResult::class,
];
protected $casts = [
'product_type' => ProductType::class,
];
}
Convering old style class/constant based enums to real php 8.1 enums results in Fatal error:
PHP Fatal error: Constant expression contains invalid operations in /var/www/html/app/Models/Results/ProductResult.php
enum ProductType: int
{
case CREDIT_CARD = 1;
case LOAN = 2;
}
class ProductResult extends Model
{
use HasChildren;
protected $childColumn = 'product_type';
protected $childTypes = [
ProductType::LOAN->value => LoanResult::class,
ProductType::CREDIT_CARD->value => CreditCardResult::class,
];
protected $casts = [
'product_type' => ProductType::class,
];
}
Are you guys still using this package ? I'm a bit hesitant to use it since there doesn't seem to be a lot of active development going on, like take this PR for instance, no response from the owner yet and it concerns me.
Let me know if anyone knows a better package or a solution to achieve same result using pure laravel/php
@HassanZahirnia
Yeah still using the package. Had to hardcode the $childTypes rather than use the Enum value. Defo a shame its not being maintained at the moment.
@calebporzio @JustSteveKing any potential for movement on this? Sorry for the tag.
@calebporzio @JustSteveKing - Tagging again to hope for some movement/feedback on this.
Sorry this has taken a while! Will get a release ready tomorrow 🙂
Sorry this has taken a while! Will get a release ready tomorrow 🙂
No worries at all! Happy to have contributed 🔥
Similar to #53, this allows the child types to be set via the
getChildTypes
method.In our application's use case, once again similar to #53, we use an enum to map the
type
to the model's class. We have agetClassMap()
static method on our enum which would reduce the amount of duplicate code if we were able to do this.