Eloquent casting. How to use a array of abstract dto? #705
Answered
by
rubenvanassche
RomainMazB
asked this question in
Q&A
-
Hi! I would like to use the abstract dto feature within an array with the nesting collection feature. I've created what looks to be the correct structure: // The abstract class
abstract class ActionConditionBase extends Data
{}
// The instantiatable class
class HasExecutedActionCondition extends ActionConditionBase implements NPCCondition
{
public function __construct(
public ActionDescription $actionDescription
)
{}
}
// The DTO storing the array of abstract/generic ActionConditionBase
class NPCAction extends Data
{
public function __construct(
public string $identifier,
public int $times = 1,
/** @var array<ActionConditionBase> */
public ?array $conditions = []
)
{}
} If I try to store this inside the database, refresh the model and try to dd it: $npc = NPC::factory()
->create([
'data' => new \App\Classes\NPCAction('talk', conditions: [
new \App\Classes\HasExecutedActionCondition(
new \App\Classes\ActionDescription($firstNpc, $actionName, 2))
])
]);
dd($npc->refresh()->data->conditions); It fails in: Error: Cannot instantiate abstract class App\Classes\ActionConditionBase Is there a way to use an array of generic of should I use a custom transformer? |
Beta Was this translation helpful? Give feedback.
Answered by
rubenvanassche
Mar 14, 2024
Replies: 1 comment 3 replies
-
I read #407 Is that unchecked checkbox the feature I'm searching for 😢 ? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah we do not support it at the moment since it requires a lot of work to get things like validation, partial includes and magical creation right