search

Home  >  Q&A  >  body text

Regarding whether the hasOne object in Laravel ORM can target arrays

Use hasOne target for db object (eloquent)

Can the target object be an array?

Or there is a package that can complete it

The following will convert the data into json for explanation
Example:
{
"id": 1,
"name": "test",
"size_id": 10,
"size": {
"id": 10,
"size": "20",
"type_id": "10",
"type": {
"id": 10,
"name": "yes"
}

}
}

The above size is the size table in the db, so I can associate and retrieve it through hasOne
But type is not the data of db table, because its value can only have two kinds: yes | no, so I made an array to get the type name
I want the content of type to be used the same as relations
I don’t know if it can be achieved

PHP中文网PHP中文网2799 days ago823

reply all(2)I'll reply

  • 淡淡烟草味

    淡淡烟草味2017-05-16 16:58:17

    Specifically, I just want to get the value easily
    I want to pass directly in the view
    $mode->size->type->name gets
    And my current approach is
    Roughly:
    $type = ['10' => 'yes' , '20' => 'no'];
    $type[$mode->size->type_id]
    I don’t think this method works

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 16:58:17

    php$model->size->type = (object) array(
        'id' => 10,
        'name' => 'yes',
    );
    

    reply
    0
  • Cancelreply