Regarding whether the hasOne object in Laravel ORM can target arrays
PHP中文网
PHP中文网 2017-05-16 16:56:17
0
2
756

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中文网

认证0级讲师

reply all(2)
淡淡烟草味

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

刘奇
php$model->size->type = (object) array(
    'id' => 10,
    'name' => 'yes',
);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!