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
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