Modify find result

Hello,

I would like to add a new key to my find result, so i do :

$responses = Response::find();
foreach ($responses as $key => $value) {
    $dependance = Dependance::find([
        'id_dependance = :id_dependance:',
        'bind' => [
            'id_dependance' => $responses[$key]->id
            ]
    ]);


    $responses[$key]->Dependance = $dependance;

But if i do this in volt :

{% for response in responses %}
    {{ response.Dependance[0]->id }}
    <?php property_exists($response, 'Dependance') ? print 'true': print 'false'; ?>
{% endfor %

The first doesn't show anything, and the second returns false all time. I've check that the Dependance::find retunr s a result, and it does.

So how do i modify the result of a find to add a new key ??

why not using relations, it sound more appropriate in your case... ? or you can use find()->toArray then add property using bracket notation



2.0k

Well i can't use relations, since the iddependance points to different tables depending on the Type column (table dependance has : id, iddependance, type, iddependof). And i've tried toArray() but then it doesn't return related models to responses in the array, only the response which is annoying;

Just use query builder.